Skip to content

[opentelemetry-instrumentation-genai-langchain] Forward agent stream cleanup - #953

Open
rads-1996 wants to merge 5 commits into
open-telemetry:mainfrom
rads-1996:langchain-agent-stream-cleanup
Open

rads-1996 wants to merge 5 commits into
open-telemetry:mainfrom
rads-1996:langchain-agent-stream-cleanup

Conversation

@rads-1996

Copy link
Copy Markdown
Contributor

Description

Forward close() and aclose() through LangChain’s agent announcement wrappers so early stream closure releases the underlying stream and finalizes telemetry. Preserve first-step announcement scoping and synchronous generator delegation.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How has this been tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. List any relevant details for your test
configuration.

  • tox -e py312-test-instrumentation-genai-langchain-latest
  • tox -e py310-test-instrumentation-genai-langchain-oldest
  • tox -e precommit and tox -e typecheck

Checklist

See CONTRIBUTING.md
for the style guide, changelog guidance, and more.

  • Followed the style guidelines of this project
  • Changelog updated if the change requires an entry
  • Unit tests added
  • Documentation updated

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-10-29 21:27 UTC

Two things need attention:

  • Required checks are failing — investigate the failures.
  • 1 review item — respond to each (e.g. link a commit, explain why not, ask a follow-up):
    • Inline threads: 1
Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Should this be with reviewers? Comment /dashboard route:reviewers to route it to them.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The localized cleanup changes have focused synchronous and asynchronous regression coverage, with no unresolved blocking findings.

No open issues 🎉

What changed in this PR

This PR fixes early stream cleanup in LangChain’s agent announcement wrappers so closing a stream releases the underlying iterator and finalizes telemetry.

Changes:

  • Forward synchronous and asynchronous cleanup while preserving first-step announcement scoping and synchronous generator delegation.
  • Add regression tests for cleanup, context restoration, and agent-span finalization.
  • Document the fix in a changelog fragment.
File Description
instrumentation/​opentelemetry-instrumentation-genai-langchain/​tests/​test_stream_context.py Tests early agent-stream closure and telemetry finalization.
instrumentation/​opentelemetry-instrumentation-genai-langchain/​tests/​test_agent_classification_corpus.py Tests cleanup and announcement withdrawal on closure, errors, and exhaustion.
instrumentation/​opentelemetry-instrumentation-genai-langchain/​src/​opentelemetry/​instrumentation/​genai/​langchain/​agent_context.py Forwards stream cleanup while preserving announcement scope and delegation.
instrumentation/​opentelemetry-instrumentation-genai-langchain/​.changelog/​953.fixed Records the stream-cleanup fix.

Review effort: Balanced

@rads-1996
rads-1996 marked this pull request as ready for review October 22, 2026 20:24
@rads-1996
rads-1996 requested a review from a team as a code owner October 22, 2026 20:24
Comment on lines +629 to +632
assert agent_span.status.status_code == StatusCode.ERROR
assert (
agent_span.attributes[error_attributes.ERROR_TYPE] == "GeneratorExit"
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An early close by the caller isn't an error, so the agent span shouldn't get error.type=GeneratorExit or an error status. To match OpenAI chat today, could we set gen_ai.response.finish_reasons=["error"] instead? We'll revisit the value once semconv decides, tracked in #963.

Comment on lines +161 to +178
delegating = False
try:
first = next(iterator)
except StopIteration:
return
entry = _push(name)
try:
first = next(iterator)
except StopIteration:
return
finally:
_pop(entry)
yield first
# yield from forwards close itself once delegation has started.
delegating = True
yield from iterator
finally:
_pop(entry)
yield first
yield from iterator
if not delegating:
close = getattr(iterator, "close", None)
if close is not None:
close()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Pregel.stream is typed as Iterator, but it's a generator. Narrowing with isinstance avoids getattr and the delegating flag (close() on a finished generator is a no-op). Needs Generator from collections.abc.

Suggested change
delegating = False
try:
first = next(iterator)
except StopIteration:
return
entry = _push(name)
try:
first = next(iterator)
except StopIteration:
return
finally:
_pop(entry)
yield first
# yield from forwards close itself once delegation has started.
delegating = True
yield from iterator
finally:
_pop(entry)
yield first
yield from iterator
if not delegating:
close = getattr(iterator, "close", None)
if close is not None:
close()
try:
entry = _push(name)
try:
first = next(iterator)
except StopIteration:
return
finally:
_pop(entry)
yield first
yield from iterator
finally:
if isinstance(iterator, Generator):
iterator.close()

Comment on lines +197 to +200
finally:
_pop(entry)
yield first
async for chunk in iterator:
yield chunk
close = getattr(iterator, "aclose", None)
if close is not None:
await close()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: same here, needs AsyncGenerator from collections.abc:

Suggested change
finally:
_pop(entry)
yield first
async for chunk in iterator:
yield chunk
close = getattr(iterator, "aclose", None)
if close is not None:
await close()
finally:
if isinstance(iterator, AsyncGenerator):
await iterator.aclose()

@rads-1996
rads-1996 force-pushed the langchain-agent-stream-cleanup branch from 6620c4b to 1b14b4e Compare October 23, 2026 15:36
@opentelemetry-pr-dashboard

Copy link
Copy Markdown

Hi @rads-1996 — just a friendly reminder that this pull request is waiting on you. The dashboard status comment has the open items and is kept current.

  • Replying is enough to hand it off — answer, explain why no change is needed, or ask a follow-up. The dashboard routes it onward once nothing on the list is waiting on you.
  • To hand it back for any other reason, including the dashboard getting this wrong, comment /dashboard route:reviewers.

This branch has not been deployed

No deployments
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.

3 participants