open-telemetry / open-telemetry/opentelemetry-python-genai
Stream wrappers cannot finalize telemetry for generator-backed streams
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 39
- Forks
- 63
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 175
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:
AsyncStreamWrapperexpects the stream to exposeasync close(), but async generators exposeaclose(). Instrumentations have to overrideaclose/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
GeneratorExitreaches afinally:
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
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.
Assessment
- Tech stack
- python
- Domain
- observability
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100