diff --git a/ddtrace/contrib/openai/patch.py b/ddtrace/contrib/openai/patch.py index 5e3bf2caead..a610cf55771 100644 --- a/ddtrace/contrib/openai/patch.py +++ b/ddtrace/contrib/openai/patch.py @@ -294,7 +294,12 @@ async def patched_endpoint(func, args, kwargs): raise finally: try: - g.send((resp, err)) + if resp is not None: + # openai responses cannot be None + # if resp is None, it is likely because the context + # of the request was cancelled, so we want that to propagate up properly + # see: https://github.com/DataDog/dd-trace-py/issues/10191 + g.send((resp, err)) except StopIteration as e: if err is None: # This return takes priority over `return resp` diff --git a/releasenotes/notes/openai-async-timeout-errors-f9ccc1adbe4ed14e.yaml b/releasenotes/notes/openai-async-timeout-errors-f9ccc1adbe4ed14e.yaml new file mode 100644 index 00000000000..17d85525559 --- /dev/null +++ b/releasenotes/notes/openai-async-timeout-errors-f9ccc1adbe4ed14e.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + openai: Fixes a bug where `asyncio.TimeoutError`s were not being propagated correctly from canceled OpenAI API requests. \ No newline at end of file diff --git a/tests/contrib/openai/test_openai_v1.py b/tests/contrib/openai/test_openai_v1.py index e14d54bca8d..ec720dfb5d3 100644 --- a/tests/contrib/openai/test_openai_v1.py +++ b/tests/contrib/openai/test_openai_v1.py @@ -1682,3 +1682,43 @@ def test_integration_service_name(openai_api_key, ddtrace_run_python_code_in_sub assert status == 0, err assert out == b"" assert err == b"" + + +async def test_openai_asyncio_cancellation(openai): + import asyncio + + import httpx + + class DelayedTransport(httpx.AsyncBaseTransport): + def __init__(self, delay: float): + self.delay = delay + self._transport = httpx.AsyncHTTPTransport() + + async def handle_async_request(self, request: httpx.Request) -> httpx.Response: + # Introduce a delay before making the actual request + await asyncio.sleep(self.delay) + return await self._transport.handle_async_request(request) + + client = openai.AsyncOpenAI(http_client=httpx.AsyncClient(transport=DelayedTransport(delay=10))) + asyncio_timeout = False + + try: + await asyncio.wait_for( + client.chat.completions.create( + model="gpt-3.5-turbo", + messages=[ + { + "role": "user", + "content": "Write a Python program that writes a Python program for a given task.", + }, + ], + user="ddtrace-test", + ), + timeout=1, + ) + except asyncio.TimeoutError: + asyncio_timeout = True + except Exception as e: + assert False, f"Unexpected exception: {e}" + + assert asyncio_timeout, "Expected asyncio.TimeoutError"