Skip to content

[BUG] Concurrent Agent Executions Fail with LiteLLM Provider - [Errno 9] Bad file descriptor #202

Open
@hellhound1988

Description

@hellhound1988

Checks

  • I have updated to the lastest minor and patch version of Strands
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

Strands Version

0.1.7

Python Version

3.12.7

Operating System

Debian Linux

Installation Method

other

Steps to Reproduce

  1. Install Strands with LiteLLM
poetry add "strands-agents[litellm]"
  1. Create a simple FastAPI app with one API method
  2. In this method, create Agent with LiteLLMModel
model = LiteLLMModel(
    model_id='bedrock/us.anthropic.claude-3-7-sonnet-20250219-v1:0'
)
researcher_agent = Agent(
    model=model,
    callback_handler=None
)
# use any prompt
async for event in researcher_agent.stream_async("...."):
  if 'message' in event:
    print(event)
  1. Call FastAPI method simultaneously multiple times to simulate concurrency

Expected Behavior

Requests are processed successfully

Actual Behavior

All requests except the very last request fail with the following error:

Give Feedback / Get Help: https://github.com/BerriAI/litellm/issues/new
LiteLLM.Info: If you need to debug this error, use `litellm._turn_on_debug()'.

  File "/usr/local/lib/python3.12/site-packages/httpcore/_sync/http11.py", line 334, in __iter__
    for chunk in self._connection._receive_response_body(**kwargs):
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/httpcore/_sync/http11.py", line 203, in _receive_response_body
    event = self._receive_event(timeout=timeout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/httpcore/_sync/http11.py", line 217, in _receive_event
    data = self._network_stream.read(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/httpcore/_backends/sync.py", line 126, in read
    with map_exceptions(exc_map):
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/contextlib.py", line 158, in __exit__
    self.gen.throw(value)
  File "/usr/local/lib/python3.12/site-packages/httpcore/_exceptions.py", line 14, in map_exceptions
    raise to_exc(exc) from exc
httpcore.ReadError: [Errno 9] Bad file descriptor

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/litellm/litellm_core_utils/streaming_handler.py", line 1491, in __next__
    chunk = next(self.completion_stream)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/litellm/llms/bedrock/chat/invoke_handler.py", line 1484, in iter_bytes
    for chunk in iterator:
                 ^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/httpx/_models.py", line 897, in iter_bytes
    for raw_bytes in self.iter_raw():
                     ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/httpx/_models.py", line 951, in iter_raw
    for raw_stream_bytes in self.stream:
                            ^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/httpx/_client.py", line 153, in __iter__
    for chunk in self._stream:
                 ^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/httpx/_transports/default.py", line 126, in __iter__
    with map_httpcore_exceptions():
         ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/contextlib.py", line 158, in __exit__
    self.gen.throw(value)
  File "/usr/local/lib/python3.12/site-packages/httpx/_transports/default.py", line 118, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.ReadError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/workspace/src/strands_api/main.py", line 83, in conduct_research
    result = await research(research_params)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/src/strands_api/research.py", line 79, in research
    async for event in researcher_agent.stream_async(prompt):
  File "/usr/local/lib/python3.12/site-packages/strands/agent/agent.py", line 435, in stream_async
    raise item
  File "/usr/local/lib/python3.12/site-packages/strands/agent/agent.py", line 418, in target_callback
    result = self._run_loop(prompt, kwargs, supplementary_callback_handler=queuing_callback_handler)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/strands/agent/agent.py", line 462, in _run_loop
    return self._execute_event_loop_cycle(invocation_callback_handler, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/strands/agent/agent.py", line 490, in _execute_event_loop_cycle
    stop_reason, message, metrics, state = event_loop_cycle(
                                           ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/strands/event_loop/event_loop.py", line 190, in event_loop_cycle
    raise e
  File "/usr/local/lib/python3.12/site-packages/strands/event_loop/event_loop.py", line 148, in event_loop_cycle
    stop_reason, message, usage, metrics, kwargs["request_state"] = stream_messages(
                                                                    ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/strands/event_loop/streaming.py", line 340, in stream_messages
    return process_stream(chunks, callback_handler, messages, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/strands/event_loop/streaming.py", line 290, in process_stream
    for chunk in chunks:
                 ^^^^^^
  File "/usr/local/lib/python3.12/site-packages/strands/types/models/model.py", line 115, in converse
    for event in response:
                 ^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/strands/models/openai.py", line 96, in stream
    for event in response:
                 ^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/litellm/litellm_core_utils/streaming_handler.py", line 1603, in __next__
    raise exception_type(
          ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 2270, in exception_type
    raise e
  File "/usr/local/lib/python3.12/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 2239, in exception_type
    raise APIConnectionError(
litellm.exceptions.APIConnectionError: litellm.APIConnectionError: BedrockException - [Errno 9] Bad file descriptor

Additional Context

If I use any other model provider (e.g. Bedrock directly), concurrent requests are handled correctly.

Possible Solution

My current workaround is to introduce random delay in the method to minimize the chance of two agents executed simultaneously - even 1 second delay is enough to avoid the error, but this approach is not sustainable.

Related Issues

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions