Skip to content
This repository was archived by the owner on Nov 11, 2026. It is now read-only.

fix(py): finish closed streams and retain async errors - #3482

Open
Stefan Wang (1fanwang) wants to merge 8 commits into
langchain-ai:mainfrom
1fanwang:fix/record-async-stream-errors
Open

Stefan Wang (1fanwang) wants to merge 8 commits into
langchain-ai:mainfrom
1fanwang:fix/record-async-stream-errors

Conversation

@1fanwang

@1fanwang Stefan Wang (1fanwang) commented Sep 2, 2026 •

Copy link
Copy Markdown

Summary

Closing a wrapped OpenAI stream releases its HTTP response but can leave the LangSmith run unfinished. Async stream failures can also lose their error, or the trace can record success before a later context error.

The fix records partial output when a stream closes and preserves the exception from the outer async context. Repeated closure does not emit another completion.

Testing Done

Real OpenAI and LangSmith clients exchange server-sent events and trace updates with a local HTTP server. These runs do not call a hosted provider.

User action Without the fix With the fix
Close a sync or async stream after its first chunk The HTTP response closes, but the trace has no completion One completed trace contains the partial output
Raise after exhausting an async stream The trace has no error and ends before the exception The trace retains the exception and ends after it
Raise inside the async context after closing the stream The trace loses the exception The trace retains the exception

The baseline is 3114edf. From the Python SDK directory, use its locked Python 3.12 test environment:

CHECKOUT=$PWD
ROOT=$(git rev-parse --show-toplevel)
BASELINE=$(mktemp -d)
git -C "$ROOT" worktree add --detach "$BASELINE" 3114edff2f41e5989724f27c0b37d3f69c6f198c
cp tests/unit_tests/test_run_helpers.py "$BASELINE/python/tests/unit_tests/test_run_helpers.py"
cp tests/unit_tests/conftest.py "$BASELINE/python/tests/unit_tests/conftest.py"
(
  cd "$BASELINE/python" &&
  UV_PROJECT_ENVIRONMENT="$CHECKOUT/.venv" UV_NO_SYNC=1 make tests \
    TEST='tests/unit_tests/test_run_helpers.py -k openai_stream_records -n 0 -p no:dotenv --tb=short --log-cli-level=INFO'
)

Without the fix:

INFO     tests.unit_tests.test_run_helpers:test_run_helpers.py:861 Collected stream run: error=None end_time=2026-10-15T19:11:12.493611+00:00 body_error_at=2026-10-15T19:11:12.495938+00:00 output='hello'
INFO     tests.unit_tests.test_run_helpers:test_run_helpers.py:962 Explicit close: async=False context_error=False transport_closed=True completed_runs=0
INFO     tests.unit_tests.test_run_helpers:test_run_helpers.py:962 Explicit close: async=True context_error=False transport_closed=True completed_runs=0
INFO     tests.unit_tests.test_run_helpers:test_run_helpers.py:962 Explicit close: async=True context_error=True transport_closed=True completed_runs=1
INFO     tests.unit_tests.test_run_helpers:test_run_helpers.py:971 Explicit close: async=True context_error=True transport_closed=True end_time=2026-10-15T19:11:12.802468+00:00 error=None output={'id': 'chatcmpl-local', 'choices': [{'index': 0, 'finish_reason': None, 'message': {'role': 'assistant', 'content': 'hello'}}], 'created': 1, 'model': 'local', 'object': 'chat.completion.chunk', 'moderation': None, 'service_tier': None, 'system_fingerprint': None, 'usage_metadata': None}
make: *** [tests] Error 1

exit=2

With the fix, from the same Python SDK directory:

make tests TEST='tests/unit_tests/test_run_helpers.py -k openai_stream_records -n 0 -p no:dotenv --tb=short --log-cli-level=INFO'
INFO     tests.unit_tests.test_run_helpers:test_run_helpers.py:861 Collected stream run: error="RuntimeError('consumer failed after exhaustion')" end_time=2026-10-15T19:13:56.991303+00:00 body_error_at=2026-10-15T19:13:56.989694+00:00 output='hello'
INFO     tests.unit_tests.test_run_helpers:test_run_helpers.py:962 Explicit close: async=False context_error=False transport_closed=True completed_runs=1
INFO     tests.unit_tests.test_run_helpers:test_run_helpers.py:971 Explicit close: async=False context_error=False transport_closed=True end_time=2026-10-15T19:13:57.030920+00:00 error=None output={'id': 'chatcmpl-local', 'choices': [{'index': 0, 'finish_reason': None, 'message': {'role': 'assistant', 'content': 'hello'}}], 'created': 1, 'model': 'local', 'object': 'chat.completion.chunk', 'moderation': None, 'service_tier': None, 'system_fingerprint': None, 'usage_metadata': None}
INFO     tests.unit_tests.test_run_helpers:test_run_helpers.py:962 Explicit close: async=True context_error=False transport_closed=True completed_runs=1
INFO     tests.unit_tests.test_run_helpers:test_run_helpers.py:971 Explicit close: async=True context_error=False transport_closed=True end_time=2026-10-15T19:13:57.070854+00:00 error=None output={'id': 'chatcmpl-local', 'choices': [{'index': 0, 'finish_reason': None, 'message': {'role': 'assistant', 'content': 'hello'}}], 'created': 1, 'model': 'local', 'object': 'chat.completion.chunk', 'moderation': None, 'service_tier': None, 'system_fingerprint': None, 'usage_metadata': None}
INFO     tests.unit_tests.test_run_helpers:test_run_helpers.py:962 Explicit close: async=True context_error=True transport_closed=True completed_runs=1
INFO     tests.unit_tests.test_run_helpers:test_run_helpers.py:971 Explicit close: async=True context_error=True transport_closed=True end_time=2026-10-15T19:13:57.110085+00:00 error="RuntimeError('consumer failed after close')" output={'id': 'chatcmpl-local', 'choices': [{'index': 0, 'finish_reason': None, 'message': {'role': 'assistant', 'content': 'hello'}}], 'created': 1, 'model': 'local', 'object': 'chat.completion.chunk', 'moderation': None, 'service_tier': None, 'system_fingerprint': None, 'usage_metadata': None}

Signed-off-by: 1fanwang <1fannnw@gmail.com>
Signed-off-by: 1fanwang <1fannnw@gmail.com>
Signed-off-by: 1fanwang <1fannnw@gmail.com>
Signed-off-by: 1fanwang <1fannnw@gmail.com>
Signed-off-by: 1fanwang <1fannnw@gmail.com>
Signed-off-by: 1fanwang <1fannnw@gmail.com>
Signed-off-by: 1fanwang <1fannnw@gmail.com>
@1fanwang Stefan Wang (1fanwang) changed the title fix(py): record errors from async streams fix(py): finish closed streams and retain async errors Sep 19, 2026
This brings the branch up to date with upstream main. The only conflict was in test_multipart_retry_matrix.py, where upstream gave its local endpoint fixture a status override per path and a list of requested paths, while this branch had moved that fixture into conftest.py. I kept the shared fixture and gave it the same two capabilities, so the upstream test is unchanged.

Signed-off-by: 1fanwang <1fannnw@gmail.com>
@thibautlehmann

Copy link
Copy Markdown
Contributor

Hey Stefan Wang (@1fanwang), thanks for this PR 🙏

This repo is archived, so it can’t be merged here anymore, the SDKs moved to:

If it's still relevant, please reopen it on the right repo !

More context in #3691

This branch has not been deployed

No deployments
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants