Skip to content

fix(langchain): don't mark spans errored when a stream is closed early - #1078

Merged
Abhijeet Prasad (AbhiPrasad) merged 2 commits into
mainfrom
abhi/sdk-797-python-langchain-integration-logs-an
Nov 6, 2026
Merged

Abhijeet Prasad (AbhiPrasad) merged 2 commits into
mainfrom
abhi/sdk-797-python-langchain-integration-logs-an

Conversation

@AbhiPrasad

Copy link
Copy Markdown
Member

🤖 This text was AI-generated.

When code stops reading a LangGraph stream partway through, Python closes the stream's generator with GeneratorExit. LangGraph passes that to on_chain_error, and BraintrustCallbackHandler logged it as error=str(error). str(GeneratorExit()) is "", so the root "LangGraph" span showed up as failed with an empty Error panel:

async for mode, chunk in graph.astream(..., config={"callbacks": [handler]}):
    if done(chunk):
        return  # -> GeneratorExit -> on_chain_error -> span error: ""

The handler now treats GeneratorExit as an intentional stop, not a failure. A new _error_for_span() helper returns None for it and str(error) for everything else. All four error callbacks use it (on_llm_error, on_chain_error, on_tool_error, on_retriever_error), because LLM streams can be stopped early the same way:

GeneratorExit  -> span ends, no error
other errors   -> span ends, error=str(error)   (unchanged)

asyncio.CancelledError is still recorded as an error. Unlike the ADK integration, this handler does not treat it as cleanup, because it can also come from timeouts.

Fixes https://linear.app/braintrustdata/issue/SDK-797

> 🤖 **This text was AI-generated.**

When code stops reading a LangGraph stream partway through, Python closes
the stream's generator with `GeneratorExit`. LangGraph passes that to
`on_chain_error`, and `BraintrustCallbackHandler` logged it as
`error=str(error)`. `str(GeneratorExit())` is `""`, so the root "LangGraph"
span showed up as failed with an empty Error panel:

```python
async for mode, chunk in graph.astream(..., config={"callbacks": [handler]}):
    if done(chunk):
        return  # -> GeneratorExit -> on_chain_error -> span error: ""
```

The handler now treats `GeneratorExit` as an intentional stop, not a
failure. A new `_error_for_span()` helper returns `None` for it and
`str(error)` for everything else. All four error callbacks use it
(`on_llm_error`, `on_chain_error`, `on_tool_error`,
`on_retriever_error`), because LLM streams can be stopped early the same
way:

```
GeneratorExit  -> span ends, no error
other errors   -> span ends, error=str(error)   (unchanged)
```

`asyncio.CancelledError` is still recorded as an error. Unlike the ADK
integration, this handler does not treat it as cleanup, because it can
also come from timeouts.

Fixes SDK-797
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-06T16:16:01.330593Z 696c053 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

> 🤖 **This text was AI-generated.**

The previous commit moved the `langgraph.graph` import to the top of
`test_callbacks.py`. The `pylint` session in `static_checks` doesn't
install langgraph, so it failed with:

```
test_callbacks.py:20:0: E0401: Unable to import 'langgraph.graph' (import-error)
```

pylint skips `import-error` when the import is inside a
`try/except ImportError` block. This puts the guarded imports back inside
the two langgraph tests.

Refs SDK-797
@AbhiPrasad
Abhijeet Prasad (AbhiPrasad) merged commit 2651d7a into main Nov 6, 2026
191 of 195 checks passed
@AbhiPrasad
Abhijeet Prasad (AbhiPrasad) deleted the abhi/sdk-797-python-langchain-integration-logs-an branch November 6, 2026 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants