langchain-ai / langchain-aws

Build LangChain Applications on AWS
MIT License
108 stars 89 forks source link

Recursion depth error: Tool calling with streaming model #80

Closed munday-tech closed 4 months ago

munday-tech commented 5 months ago

When setting the Bedrock model to streaming=True and tool calls enabled, it causes a recursion depth error due to stream and generate constantly pointing back to each other.

  File "C:\Users\..\AppData\Local\pypoetry\Cache\virtualenvs\..\lib\site-packages\langchain_aws\chat_models\bedrock.py", line 432, in _stream
    result = self._generate(
  File "C:\Users\..\AppData\Local\pypoetry\Cache\virtualenvs\..\lib\site-packages\langchain_aws\chat_models\bedrock.py", line 501, in _generate
    for chunk in self._stream(messages, stop, run_manager, **kwargs):
  File "C:\Users\..\AppData\Local\pypoetry\Cache\virtualenvs\..\lib\site-packages\langchain_aws\chat_models\bedrock.py", line 432, in _stream
    result = self._generate(
  File "C:\Users\..\AppData\Local\pypoetry\Cache\virtualenvs\..\lib\site-packages\langchain_aws\chat_models\bedrock.py", line 501, in _generate
    for chunk in self._stream(messages, stop, run_manager, **kwargs):
  File "C:\Users\..\AppData\Local\pypoetry\Cache\virtualenvs\..\lib\site-packages\langchain_aws\chat_models\bedrock.py", line 432, in _stream
    result = self._generate(
  File "C:\Users\..\AppData\Local\pypoetry\Cache\virtualenvs\..\lib\site-packages\langchain_aws\chat_models\bedrock.py", line 501, in _generate
    for chunk in self._stream(messages, stop, run_manager, **kwargs):
  File "C:\Users\..\AppData\Local\pypoetry\Cache\virtualenvs\..\lib\site-packages\langchain_aws\chat_models\bedrock.py", line 427, in _stream
    provider = self._get_provider()
  File "C:\Users\..\AppData\Local\pypoetry\Cache\virtualenvs\..\lib\site-packages\langchain_aws\llms\bedrock.py", line 570, in _get_provider
    if self.model_id.startswith("arn"):
RecursionError: maximum recursion depth exceeded while calling a Python object

llm = ChatBedrock(client=bedrock_client, model_id=model, model_kwargs=model_kwargs, streaming=True)

def _stream(
...

        if "claude-3" in self._get_model():
            if _tools_in_params({**kwargs}):
                result = self._generate(
                    messages, stop=stop, run_manager=run_manager, **kwargs
                )

_stream points back to self._generate if model is claude 3 and tools exist;

    def _generate(
...
        if self.streaming:
            response_metadata: List[Dict[str, Any]] = []
            for chunk in self._stream(messages, stop, run_manager, **kwargs):
                completion += chunk.text
                response_metadata.append(chunk.message.response_metadata)
                if "tool_calls" in chunk.message.additional_kwargs.keys():
                    tool_calls = chunk.message.additional_kwargs["tool_calls"]
            llm_output = _combine_generation_info_for_llm_result(
                response_metadata, provider_stop_reason_code
            )

_generate points back to _stream() if self.streaming is enabled.

baskaryan commented 4 months ago

Do folks see the same issue when using ChatBedrockConverse or does that work?