Skip to content

Fix lazy request iterator capture in Anthropic instrumentation - #873

Merged
lmolkova merged 7 commits into
open-telemetry:mainfrom
lmolkova:fix/anthropic-iterator-capture
Oct 13, 2026
Merged

lmolkova merged 7 commits into
open-telemetry:mainfrom
lmolkova:fix/anthropic-iterator-capture

Conversation

@lmolkova

@lmolkova lmolkova commented Oct 11, 2026 •

Copy link
Copy Markdown
Member

Fix content capture for generators in Anthropic messages, nested content, system instructions, and tools. Add a shared IteratorCapture helper that records values as the SDK consumes them, preserving partial input when a request fails.

Instrumentations can use the helper when content capture is enabled:

from opentelemetry.util.genai.iterator_capture import IteratorCapture

capture = IteratorCapture.from_property(kwargs, "messages", enabled=capture_content)
try:
    response = sdk_call(**kwargs)
finally:
    captured_messages = capture.extract(get_input_messages)

The helper wraps synchronous iterators inside built-in dictionaries, lists, and tuples. File objects pass through unchanged. Async iterators and custom containers are not captured.

Anthropic uses this automatically when message content capture is enabled.

@lmolkova
lmolkova requested a balanced review from Copilot October 11, 2026 18:05
@opentelemetry-pr-dashboard

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

Copy link
Copy Markdown

Pull request dashboard status

Merged · refreshed 2026-10-13 15:33 UTC

Status above doesn't look right?
  • Anything look wrong? 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.

🟡 Changes recommended

Request-content extraction can mask SDK failures, and the new guidance contains a broken section link.

2 open findings
What changed in this PR

Adds lazy iterator capture to util-genai and integrates it with Anthropic request-content telemetry.

Changes:

  • Introduces and tests IteratorCapture.
  • Captures Anthropic message, system, tool, and nested-content iterators.
  • Updates dependencies, documentation, guidance, and changelogs.
File Description
util/​opentelemetry-util-genai/​src/​opentelemetry/​util/​genai/​iterator_capture.py Implements lazy iterator capture.
util/​opentelemetry-util-genai/​tests/​test_iterator_capture.py Tests capture behavior and transparency.
util/​opentelemetry-util-genai/​.changelog/​873.added Records the new utility.
instrumentation/​.../​anthropic/​patch.py Integrates capture into Anthropic wrappers.
instrumentation/​.../​anthropic/​wrappers.py Extracts captured stream-manager input.
instrumentation/​.../​anthropic/​messages_extractors.py Applies snapshots to invocations.
instrumentation/​.../​tests/​test_iterator_transparency.py Tests SDK transparency across variants.
instrumentation/​.../​tests/​test_sync_messages.py Adds generator telemetry assertions.
instrumentation/​.../​tests/​test_messages_extractors.py Tests incomplete-message handling.
instrumentation/​.../​tests/​requirements.oldest.txt Installs the current local util floor.
instrumentation/​.../​pyproject.toml Raises the util-genai dependency floor.
instrumentation/​.../​README.rst Documents lazy iterator capture.
instrumentation/​.../​.changelog/​873.fixed Records the Anthropic fix.
AGENTS.md Adds iterator-capture contributor guidance.
.github/​instructions/​instrumentation.instructions.md Adds corresponding review guidance.

🧠 Review effort: Balanced


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

Comment thread .github/instructions/instrumentation.instructions.md Outdated
@lmolkova
lmolkova marked this pull request as ready for review October 11, 2026 19:55
@lmolkova
lmolkova requested a review from a team as a code owner October 11, 2026 19:55
@lmolkova
lmolkova added this pull request to the merge queue Oct 13, 2026
Merged via the queue into open-telemetry:main with commit 14413b9 Oct 13, 2026
79 checks passed
@lmolkova
lmolkova deleted the fix/anthropic-iterator-capture branch October 13, 2026 15:32
sangkyoonnam added a commit to sangkyoonnam/opentelemetry-python-genai that referenced this pull request Oct 13, 2026
RichardoMrMu pushed a commit to RichardoMrMu/opentelemetry-python-genai that referenced this pull request Oct 16, 2026
…try#866)

* fix(bedrock): keep request extraction errors from failing the call

* chore(bedrock): add changelog fragment for open-telemetry#866

* feat(util-genai): add suppress_extraction_errors and use it in bedrock

Assisted-by: Claude Opus 5.5

* fix: use suppress_extraction_errors in openai, anthropic, portkey and google-genai

Assisted-by: Claude Opus 5.5

* chore: add changelog fragments for open-telemetry#866

* fix(smolagents): keep request extraction errors from failing the call

Assisted-by: Claude Opus 5.5

* refactor: drop suppress_extraction_errors

Assisted-by: Claude Opus 5.5

* fix: handle malformed request inputs in extractors

Assisted-by: Claude Opus 5.5

* chore: add changelog fragments for open-telemetry#866

* fix: tighten interactions fallback, empty contents and numeric parsing

Assisted-by: Claude Opus 5.5

* chore: narrow the anthropic changelog fragment after open-telemetry#873

* fix(portkey): check numeric request parameter types instead of catching conversion errors
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