Stream wrappers cannot finalize telemetry for generator-backed streams
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:
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.
- Dominant language
- Python
- Stars
- 47
- Forks
- 102
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 111
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing 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.
More from open-telemetry/opentelemetry-python-genai
-
`opentelemetry-instrumentation-openai-v2` instrumentor entry point fails to loadPossibly taken @lmolkova claimed this 1 day ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
open-telemetry/opentelemetry-python-genai#997 · 1 comment ·
Maintainers usually reply within 1 day
-
Support disabling OpenAI Agents native trace export through an environment variablePossibly taken @123liuziming claimed this 10 days ago. Openenhancement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
open-telemetry/opentelemetry-python-genai#922 · 3 comments ·
Maintainers usually reply within 1 day
-
fix(smolagents): accept tuple inputs for messages, content, and task imagesPossibly taken @SAMurai-16 claimed this 13 days ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 82/100
open-telemetry/opentelemetry-python-genai#905 ·
Maintainers usually reply within 1 day
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
open-telemetry/opentelemetry-python-genai#857 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
[genai-bedrock] invoke_model body passed as a file-like object: no request parameters or input messages are recordedPossibly taken @SyedTashfin claimed this 20 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
open-telemetry/opentelemetry-python-genai#848 ·
Maintainers usually reply within 1 day
All issues in open-telemetry/opentelemetry-python-genai
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
aws-samples/sample-per-model-bedrock#639 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 86/100
awslabs/visual-asset-management-system#937 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
project-codeflare/codeflare-sdk#1195 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
pandas-dev/pandas-stubs#2051 ·
Maintainers usually reply within 1 day