Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Stream wrappers cannot finalize telemetry for generator-backed streams

Open
#386 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
48/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
observability

Research direction

Start by reading the SyncStreamWrapper and AsyncStreamWrapper implementations in opentelemetry.util.genai.stream, then review the generator-backed usage described for qwen-agent and the proposed qwenpaw package in #311. Done means generator-backed streams finalize telemetry when consumption stops early while preserving the original async_generator shape and handling async-generator close behavior.

Written by the indexing model from the issue text.

Description

SyncStreamWrapper / AsyncStreamWrapper are wrapt proxies that finalize telemetry from close() / __exit__ / __aexit__ and from iteration reaching its end. When the wrapped stream is a generator and the caller stops consuming it early, none of those run: the caller drops the proxy, and GC finalizes the underlying generator directly, so the span is never ended.

This is normal traffic for agent instrumentations that wrap generator-returning APIs. For example QwenPaw's ACP server breaks out of the async for when the client cancels a turn:

async for msg, _is_last in runner.query_handler(msgs, request=request):
    if cancel_event.is_set():
        break

Effects: the invoke_agent span is never exported and never recorded in gen_ai.client.operation.duration, and because the invocation attached its context at start and only detaches on finalize, later spans in the same task become children of the span that never ended.

Two related gaps:

  • AsyncStreamWrapper expects the stream to expose async close(), but async generators expose aclose(). Instrumentations have to override aclose / close / __aexit__ to bridge that.
  • No proxy-based wrapper can catch GC teardown at all. Catching it requires the wrapper to itself be an async generator, so GeneratorExit reaches a finally:
async def _driver():
    try:
        async for item in gen:
            yield item
    finally:
        ...finalize telemetry...
        await gen.aclose()

This shape does finalize on the break path above, and it keeps the returned object an async_generator, which is closer to the SDK's original type than a proxy is.

Suggest a generator-aware wrapper in opentelemetry.util.genai.stream so instrumentations wrapping generator APIs (qwen-agent, the proposed qwenpaw package in #311) get correct finalization instead of each working around it.

Dominant language
Python
Stars
47
Forks
102
Avg merge
2d 13h
Merged PRs (30d)
111

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from open-telemetry/opentelemetry-python-genai

All issues in open-telemetry/opentelemetry-python-genai

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.