VRSEN / agency-swarm

The only reliable agent framework built on top of the latest OpenAI Assistants API.
https://vrsen.github.io/agency-swarm/
MIT License
2.05k stars 537 forks source link

httpx.ReadTimeout: The read operation timed out #99

Closed ha-zhcool closed 2 months ago

ha-zhcool commented 3 months ago

I'm constantly facing the same error after few request and the agency script stops working and the interface stops responding. I should always restart the agency and start over again. Could you please help fixing this issue

Exception in thread Thread-9 (get_completion_stream):
Traceback (most recent call last):
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/httpx/_transports/default.py", line 66, in map_httpcore_exceptions
    yield
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/httpx/_transports/default.py", line 110, in __iter__
    for part in self._httpcore_stream:
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py", line 367, in __iter__
    raise exc from None
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py", line 363, in __iter__
    for part in self._stream:
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/httpcore/_sync/http11.py", line 349, in __iter__
    raise exc
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/httpcore/_sync/http11.py", line 341, in __iter__
    for chunk in self._connection._receive_response_body(**kwargs):
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/httpcore/_sync/http11.py", line 210, in _receive_response_body
    event = self._receive_event(timeout=timeout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/httpcore/_sync/http11.py", line 224, in _receive_event
    data = self._network_stream.read(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/httpcore/_backends/sync.py", line 124, in read
    with map_exceptions(exc_map):
  File "/usr/lib/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/httpcore/_exceptions.py", line 14, in map_exceptions
    raise to_exc(exc) from exc
httpcore.ReadTimeout: The read operation timed out

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

Traceback (most recent call last):
  File "/usr/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.11/threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/agency_swarm/agency/agency.py", line 143, in get_completion_stream
    next(gen)
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/agency_swarm/threads/thread.py", line 84, in get_completion
    item = next(output)
           ^^^^^^^^^^^^
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/agency_swarm/agency/agency.py", line 733, in run
    yield next(gen)
          ^^^^^^^^^
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/agency_swarm/threads/thread.py", line 65, in get_completion
    self._create_run(recipient_agent, event_handler)
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/agency_swarm/threads/thread.py", line 152, in _create_run
    stream.until_done()
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/openai/lib/streaming/_assistants.py", line 102, in until_done
    consume_sync_iterator(self)
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/openai/_utils/_streams.py", line 6, in consume_sync_iterator
    for _ in iterator:
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/openai/lib/streaming/_assistants.py", line 69, in __iter__
    for item in self._iterator:
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/openai/lib/streaming/_assistants.py", line 404, in __stream__
    for event in stream:
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/openai/_streaming.py", line 46, in __iter__
    for item in self._iterator:
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/openai/_streaming.py", line 61, in __stream__
    for sse in iterator:
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/openai/_streaming.py", line 53, in _iter_events
    yield from self._decoder.iter(self.response.iter_lines())
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/openai/_streaming.py", line 287, in iter
    for line in iterator:
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/httpx/_models.py", line 862, in iter_lines
    for text in self.iter_text():
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/httpx/_models.py", line 849, in iter_text
    for byte_content in self.iter_bytes():
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/httpx/_models.py", line 828, in iter_bytes
    for raw_bytes in self.iter_raw():
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/httpx/_models.py", line 886, in iter_raw
    for raw_stream_bytes in self.stream:
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/httpx/_client.py", line 123, in __iter__
    for chunk in self._stream:
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/httpx/_transports/default.py", line 109, in __iter__
    with map_httpcore_exceptions():
  File "/usr/lib/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/home/user/AI/agency-swarm/.agency/lib/python3.11/site-packages/httpx/_transports/default.py", line 83, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.ReadTimeout: The read operation timed out
VRSEN commented 3 months ago

Hey, I have also been having this issue lately. This can happen for 2 reasons:

  1. OpenAI API is having delays.
  2. Your agent calls 2 send message tools at once.

I fixed the second reason and will push the fix in the next release.

In the meantime, try increasing the timeout with:

import openai
import httpx
from agency_swarm.util.oai import set_openai_client

client = openai.OpenAI(api_key=<YOUR_OPENAI_KEY>, 
                timeout=httpx.Timeout(60.0, read=10.0, write=15.0, connect=5.0), 
                max_retries=5)

set_openai_client(client)                                                  

To fix the second, you can also add in shared instructions "Only send messages to 1 agent at a time."

ha-zhcool commented 3 months ago

Thanks for your reply. In which file should I increase the timeout? the agency file or for each agent?

VRSEN commented 2 months ago

Please upgrade to the latest release. I played with these timeouts and set default values that completely eliminate these issues.

Phuzzled commented 1 week ago

Please upgrade to the latest release. I played with these timeouts and set default values that completely eliminate these issues.

Running the latest build and just had the same issue:

I will now proceed to create these tools within the system. Traceback (most recent call last): File "C:\Python312\Lib\site-packages\httpcore_exceptions.py", line 10, in map_exceptions yield File "C:\Python312\Lib\site-packages\httpcore_backends\sync.py", line 126, in read return self._sock.recv(max_bytes) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python312\Lib\ssl.py", line 1233, in recv return self.read(buflen) ^^^^^^^^^^^^^^^^^ File "C:\Python312\Lib\ssl.py", line 1106, in read return self._sslobj.read(len) ^^^^^^^^^^^^^^^^^^^^^^ TimeoutError: The read operation timed out

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

Traceback (most recent call last): File "C:\Python312\Lib\site-packages\httpx_transports\default.py", line 66, in map_httpcore_exceptions yield File "C:\Python312\Lib\site-packages\httpx_transports\default.py", line 110, in iter for part in self._httpcore_stream: File "C:\Python312\Lib\site-packages\httpcore_sync\connection_pool.py", line 361, in iter for part in self._stream: File "C:\Python312\Lib\site-packages\httpcore_sync\http11.py", line 337, in iter raise exc File "C:\Python312\Lib\site-packages\httpcore_sync\http11.py", line 329, in iter for chunk in self._connection._receive_response_body(**kwargs): File "C:\Python312\Lib\site-packages\httpcore_sync\http11.py", line 198, in _receive_response_body event = self._receive_event(timeout=timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python312\Lib\site-packages\httpcore_sync\http11.py", line 212, in _receive_event data = self._network_stream.read( ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python312\Lib\site-packages\httpcore_backends\sync.py", line 124, in read with map_exceptions(exc_map): File "C:\Python312\Lib\contextlib.py", line 158, in exit self.gen.throw(value) File "C:\Python312\Lib\site-packages\httpcore_exceptions.py", line 14, in map_exceptions raise to_exc(exc) from exc httpcore.ReadTimeout: The read operation timed out

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

Traceback (most recent call last): File "", line 198, in _run_module_as_main File "", line 88, in _run_code File "C:\Python312\Scripts\agency-swarm.exe__main.py", line 7, in File "C:\Python312\Lib\site-packages\agency_swarm\cli.py", line 51, in main agency.run_demo() File "C:\Python312\Lib\site-packages\agency_swarm\agency\agency.py", line 515, in run_demo self.get_completion_stream(message=text, event_handler=TermEventHandler, recipient_agent=recipient_agent) File "C:\Python312\Lib\site-packages\agency_swarm\agency\agency.py", line 147, in get_completion_stream next(gen) File "C:\Python312\Lib\site-packages\agency_swarm\threads\thread.py", line 91, in get_completion item = next(output) ^^^^^^^^^^^^ File "C:\Python312\Lib\site-packages\agency_swarm\agency\agency.py", line 801, in run yield next(gen) ^^^^^^^^^ File "C:\Python312\Lib\site-packages\agency_swarm\threads\thread.py", line 91, in get_completion item = next(output) ^^^^^^^^^^^^ File "C:\Python312\Lib\site-packages\agency_swarm\agency\agency.py", line 801, in run yield next(gen) ^^^^^^^^^ File "C:\Python312\Lib\site-packages\agency_swarm\threads\thread.py", line 69, in get_completion self._create_run(recipient_agent, additional_instructions, event_handler) File "C:\Python312\Lib\site-packages\agency_swarm\threads\thread.py", line 196, in _create_run stream.until_done() File "C:\Python312\Lib\site-packages\openai\lib\streaming_assistants.py", line 102, in until_done consume_sync_iterator(self) File "C:\Python312\Lib\site-packages\openai_utils_streams.py", line 6, in consume_synciterator for in iterator: File "C:\Python312\Lib\site-packages\openai\lib\streaming_assistants.py", line 69, in iter for item in self._iterator: File "C:\Python312\Lib\site-packages\openai\lib\streaming_assistants.py", line 404, in stream for event in stream: File "C:\Python312\Lib\site-packages\openai_streaming.py", line 46, in iter for item in self._iterator: File "C:\Python312\Lib\site-packages\openai_streaming.py", line 61, in stream for sse in iterator: File "C:\Python312\Lib\site-packages\openai_streaming.py", line 53, in _iter_events yield from self._decoder.iter(self.response.iter_lines()) File "C:\Python312\Lib\site-packages\openai_streaming.py", line 287, in iter for line in iterator: File "C:\Python312\Lib\site-packages\httpx_models.py", line 862, in iter_lines for text in self.iter_text(): File "C:\Python312\Lib\site-packages\httpx_models.py", line 849, in iter_text for byte_content in self.iter_bytes(): File "C:\Python312\Lib\site-packages\httpx_models.py", line 828, in iter_bytes for raw_bytes in self.iter_raw(): File "C:\Python312\Lib\site-packages\httpx_models.py", line 886, in iter_raw for raw_stream_bytes in self.stream: File "C:\Python312\Lib\site-packages\httpx_client.py", line 123, in iter for chunk in self._stream: File "C:\Python312\Lib\site-packages\httpx_transports\default.py", line 109, in iter with map_httpcore_exceptions(): File "C:\Python312\Lib\contextlib.py", line 158, in exit__ self.gen.throw(value) File "C:\Python312\Lib\site-packages\httpx_transports\default.py", line 83, in map_httpcore_exceptions raise mapped_exc(message) from exc httpx.ReadTimeout: The read operation timed out