Help me solve this python issue I am having or find an alternative to do it. I am making a LangGraph python project, and I need to mock the llm.invoke method so I can test when a value error as occurred. This is my code and output:

import unittest
from unittest.mock import AsyncMock, patch
from langchain_core.prompts import PromptTemplate
from langchain_core.runnables import RunnableLambda
from langchain_core.runnables.retry import RunnableRetry
from langchain_openai import ChatOpenAI
import os

# Create an instance of the LLM class
llm = ChatOpenAI(
    model="meta-llama/llama-3.1-8b-instruct:free",
    openai_api_key=os.environ.get("API_KEY"), 
    openai_api_base="...",
)

# Define the mock function
async def mock_invoke(*args, **kwargs):
    raise ValueError({
        'message': '{"error":{"code":null,"message":"Rate limit exceeded","param":null,"type":"rate_limit_exceeded"}}\n',
        'code': 429
    })

# Method 1
async def test_method_1():
    with patch('langchain_openai.ChatOpenAI.invoke', new_callable=AsyncMock) as mock_invoke:
        mock_invoke.side_effect = mock_invoke

        # Define prompt and model
        template = PromptTemplate.from_template("Tell me a joke about {topic}.")

        # Add retry logic to the model invocation
        runnable_with_retries = llm.with_retry(
            retry_if_exception_type=(ValueError,),  # Retry only on ValueError exceptions
            wait_exponential_jitter=True,           # Enable jitter for backoff timing
            stop_after_attempt=3                    # Maximum 3 attempts
        )

        # Create a chain with the template and the retry-enabled model
        chain = template | runnable_with_retries

        # Invoke the chain with input and assert the result
        print("Before invoking chain")
        try:
            await chain.invoke({"topic": "programmers"})
        except ValueError as e:
            print(f"Method 1: Caught expected exception: {e}")
        print("After invoking chain")

# Method 2
async def test_method_2():
    with patch('langchain_openai.ChatOpenAI.invoke', new_callable=AsyncMock) as mock_invoke:
        mock_invoke.side_effect = mock_invoke

        # Wrap the model invocation in a Runnable
        runnable = RunnableLambda(lambda input: llm.invoke(input))

        # Configure retries
        runnable_with_retries = RunnableRetry(
            bound=runnable,
            retry_exception_types=(ValueError,),   # Retry only on ValueError
            max_attempt_number=3,                  # Retry up to 3 times
            wait_exponential_jitter=True           # Add jitter to backoff
        )

        # Invoke the runnable with retries and assert the result
        print("Before invoking runnable_with_retries")
        try:
            await runnable_with_retries.invoke("Tell me a joke.")
        except ValueError as e:
            print(f"Method 2: Caught expected exception: {e}")
        print("After invoking runnable_with_retries")

# Run the tests
await test_method_1()
await test_method_2()

Before invoking chain
---------------------------------------------------------------------------
RecursionError                            Traceback (most recent call last)
Cell In[42], line 67
     64         print("After invoking runnable_with_retries")
     66 # Run the tests
---> 67 await test_method_1()
     68 await test_method_2()

Cell In[42], line 37
     35 print("Before invoking chain")
     36 try:
---> 37     await chain.invoke({"topic": "programmers"})
     38 except ValueError as e:
     39     print(f"Method 1: Caught expected exception: {e}")

File ~\AppData\Local\anaconda3\envs\p3-12-4\Lib\unittest\mock.py:2273, in AsyncMockMixin._execute_mock_call(self, *args, **kwargs)
   2271         raise result
   2272 elif iscoroutinefunction(effect):
-> 2273     result = await effect(*args, **kwargs)
   2274 else:
   2275     result = effect(*args, **kwargs)

File ~\AppData\Local\anaconda3\envs\p3-12-4\Lib\unittest\mock.py:2273, in AsyncMockMixin._execute_mock_call(self, *args, **kwargs)
   2271         raise result
   2272 elif iscoroutinefunction(effect):
...
    296 def isclass(object):
    297     """Return true if the object is a class."""
--> 298     return isinstance(object, type)

RecursionError: maximum recursion depth exceeded

the only way I found to make it runnable, is to delete mock_invoke.side_effect = mock_invoke, and it doesn't give me errors, but instead it doesn't do anything.