Skip to content

fix(openai): materialize generator messages/tools before wrapped call (#825) - #826

Open
Niranjan-png wants to merge 3 commits into
open-telemetry:mainfrom
Niranjan-png:fix/issue-825-generator-consumed
Open

Niranjan-png wants to merge 3 commits into
open-telemetry:mainfrom
Niranjan-png:fix/issue-825-generator-consumed

Conversation

@Niranjan-png

Copy link
Copy Markdown

Fixes open-telemetry#825. Prevents one-shot iterables from being consumed during
content capture, which left the SDK request empty.

- Materialize messages and tools to list before consumption
- Update kwargs so the wrapped call sees the same materialized inputs
Copilot AI balanced review requested due to automatic review settings October 1, 2026 17:56
@Niranjan-png
Niranjan-png requested a review from a team as a code owner October 1, 2026 17:56
@linux-foundation-easycla

linux-foundation-easycla Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

CLA Not Signed

@opentelemetry-pr-dashboard

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

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-10-02 17:47 UTC

Investigate required status check failures.

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.

Copilot review overview

🟡 Changes recommended

Rebinding local kwargs leaves the caller’s dictionary unchanged, so generators remain exhausted, and regression tests are absent.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

This PR attempts to prevent OpenAI message/tool iterators from being consumed during content capture.

Changes:

  • Materializes non-list/tuple message and tool iterables.
  • Attempts to pass materialized values to the wrapped SDK call.
File Description
instrumentation/​opentelemetry-instrumentation-genai-openai/​src/​opentelemetry/​instrumentation/​genai/​openai/​utils.py Adds iterable materialization before telemetry extraction.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +188 to +190
# Materialize one-shot iterables so they aren't consumed before
# the wrapped API call (fixes #825).
messages_raw = kwargs.get("messages", [])
…telemetry#825)

- Materialize generator/map-backed messages and tools in patch.py
  traced_method before passing to create_chat_invocation and wrapped.
- Reverts incorrect local-only kwargs fix in utils.py.
- Adds regression tests for message and tool generators.

Fixes open-telemetry#825
def test_materialization_preserves_generator():
MSGS = [{"role":"system","content":"x"},{"role":"user","content":"hi"}]
generator = (m for m in MSGS)
materialized = list(generator) if not isinstance(generator, (list, tuple)) else generator

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.

This only verifies that Python’s list() consumes a generator; it doesn’t exercise the instrumented wrapper or confirm the SDK receives the materialized messages. The test would still pass if the production fix were removed. Could we test through a mock OpenAI transport and assert the request contains all messages? Please cover tools as well, including the async wrapper.

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

Development

Successfully merging this pull request may close these issues.

[genai-openai] With content capture enabled, generator/iterator messages and tools are consumed and the request is sent empty

3 participants