Skip to content

fix(google-generativeai): record input messages for tuple, Content and dict contents - #4773

Draft
doronkopit5 wants to merge 1 commit into
mainfrom
heal/issue-4591-a1
Draft

doronkopit5 wants to merge 1 commit into
mainfrom
heal/issue-4591-a1

Conversation

@doronkopit5

@doronkopit5 doronkopit5 commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Fixes #4591

Root cause

`set_input_attributes_sync` and `set_input_attributes` in span_utils.py checked `isinstance(contents, str)` and then `isinstance(contents, list)`, with no else branch. google.genai also accepts a tuple, a single `types.Content` or a ContentDict as `contents`. Any of those fell through both checks, so no messages were built and `gen_ai.input.messages` was never set. Nothing was logged, so the span looked complete but had no prompt.

Change

`gen_ai.input.messages` is now recorded when `contents` is a tuple, a single `types.Content` or a dict. Before, it was silently missing.

- **Fix:** new helper `_contents_as_list` in `span_utils.py`. A list or tuple becomes a list; a single dict or any object with `.parts` becomes a one-element list; anything else gives an empty list (nothing recorded, as before).
- Both `set_input_attributes_sync` and async `set_input_attributes` use it for non-str `contents`. The str branch, per-item processing and role normalisation are unchanged. Both run inside `@dont_throw` before the wrapped call, covering sync/async `generate_content` and `generate_content_stream`.
- **Tests:** new `tests/test_input_contents.py`, a sync and an async test parametrized over tuple, `types.Content` and dict, using a mocked span (no cassette needed: input comes from request kwargs). All 6 cases failed before the fix and pass after; the other test files still pass and ruff is clean.

Behaviour changes: spans now include `gen_ai.input.messages` when `contents` is a tuple, a single `types.Content` (or any object with `.parts`) or a dict.
Not addressed: event mode (`emit_message_events`) has the same gap; the maintainer made it a follow-up. A bare Part, File or PIL image passed as `contents` is still not wrapped, per the maintainer's narrow default.

Scope

  • slice 1 of 1
  • covers rows: 4 of 4 in rows

Out of scope:

  • emit_message_events tuple / single Content handling (event mode): The maintainer ruled it a follow-up PR (q1).
  • Wrapping a bare Part, File or PIL image passed as contents: The maintainer chose the narrow default (q2): only a dict or an object with .parts is wrapped.
  • Positional-args path (_process_argument(_sync)) tuples: generate_content(_stream) takes keyword-only args, so contents never arrives in args.
  • Merging bare Parts into one user Content as the SDK does: A behaviour change beyond this bug.
  • Stream spans not ended on close/aclose/__del__: An unrelated lifecycle bug.
  • New VCR cassettes: Input messages are built from request kwargs before the API call. No response affects them.

Regression evidence

Each test below failed on the base commit and passes with this change, run offline in the heal harness:

  • opentelemetry-instrumentation-google-generativeai:tests/test_input_contents.py::test_async_contents_shapes_recorded (failed before: assertion)
  • opentelemetry-instrumentation-google-generativeai:tests/test_input_contents.py::test_sync_contents_shapes_recorded (failed before: assertion)

Package suites: no new failures against the offline baseline.
Ruff: clean.

Telemetry evidence

  • opentelemetry\-instrumentation\-google\-generativeai: no span capture for these tests

Review

Adds `_contents_as_list` in span_utils.py, used by both set_input_attributes_sync and the async set_input_attributes. A tuple is now recorded like a list, and a single dict or Content-like object as a one-element list. Anything else still records nothing, per the maintainer's narrow default. The helper runs inside @dont_throw before the wrapped call, so all four generate_content(_stream) entry points are covered. Sync and async tests over tuple, Content and dict assert the full input messages and failed on the base. Event mode is a follow-up, as decided.

Before merging

  • CI green: Lint, Build Packages, Test Packages (3.10, 3.11, 3.12)
  • Approved by a maintainer other than the author
  • CodeRabbit review read

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of Google Generative AI input contents so tuples, individual dictionaries, and content objects are processed consistently in both synchronous and asynchronous calls.

@doronkopit5

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Input attribute processing now normalizes tuple and single-item contents values alongside lists. Tests cover synchronous and asynchronous handling of tuple, dictionary, and Content inputs.

Changes: Contents input attributes

Layer / File(s) Summary
Normalize contents and record input attributes
packages/opentelemetry-instrumentation-google-generativeai/opentelemetry/instrumentation/google_generativeai/span_utils.py, packages/opentelemetry-instrumentation-google-generativeai/tests/test_input_contents.py
A shared helper converts lists and tuples to lists, wraps dictionaries and objects with a parts attribute, and returns an empty list for other values. Both synchronous and asynchronous input processing use the helper. Tests check recorded input messages for tuple, dictionary, and Content inputs.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: avivhalfon, nirga, luizdmm

Merge Risk: Minimal · up to 91e29

This change records input messages when contents is a tuple, a single dict, or a single Content object, which previously left the prompt attribute missing. The change is small and covered by sync and async tests, and I found no merge-blocking risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix(google-generativeai): record input messages for tuple, Content and dict contents" matches the main change. The PR makes set_input_attributes in span_utils.py (sync and async) accept tup…
Linked Issues check Passed Issue #4591 requires that gen_ai.input.messages be recorded when contents is a tuple, and that a single types.Content or dict be handled as a one-element list. At the reviewed head, `span_utils.…
Out of Scope Changes check Passed The changes are limited to the Google GenAI instrumentation package. They consist of the _contents_as_list helper in span_utils.py, its use for non-str contents in the sync and async input-attri…
Full details: Title check

Explanation

The title "fix(google-generativeai): record input messages for tuple, Content and dict contents" matches the main change. The PR makes set_input_attributes in span_utils.py (sync and async) accept tuples, single types.Content-like objects with parts, and dicts, and it adds tests for those inputs. The title is short, specific, and names the affected package, the fix, and the input shapes. It does not mention the out-of-scope event-mode work, which is acceptable.

Full details: Linked Issues check

Explanation

Issue #4591 requires that gen_ai.input.messages be recorded when contents is a tuple, and that a single types.Content or dict be handled as a one-element list. At the reviewed head, span_utils.py defines _contents_as_list, which returns list(contents) for list or tuple, [contents] for a dict or an object with .parts, and [] otherwise. The PR summary and PR description state that both set_input_attributes_sync and async set_input_attributes call this helper for non-str contents. The str branch, per-item processing, and role normalization are unchanged. The added tests in tests/test_input_contents.py cover sync and async tuple, types.Content, and dict inputs. The PR reports that these tests failed on the base and pass with the change. I did not read the call sites inside set_input_attributes directly, because the file read was truncated. The call-site wiring rests on the PR description and summary, and the helper itself was confirmed in the source. Event mode and bare Part, File, or PIL image wrapping are excluded by the issue scope, so they do not count as unmet requirements.

Full details: Out of Scope Changes check

Explanation

The changes are limited to the Google GenAI instrumentation package. They consist of the _contents_as_list helper in span_utils.py, its use for non-str contents in the sync and async input-attribute functions, and a new test module. Each change supports the #4591 objective of recording input messages for tuple, Content, and dict contents. The PR's listed out-of-scope items (event mode, bare Part wrapping, positional-args path, stream span lifecycle, new cassettes) are not implemented in the reviewed diff, as far as the visible evidence shows. No unrelated change was identified. The truncated file read leaves the call-site details unverified, as noted in the linked-issue assessment.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2a7d1889-57ed-4725-9238-d0902750112a
Commits

Reviewing files that changed from the base of the PR and between b870d8a and 91e29d1.

Files selected for processing: 2

  • packages/opentelemetry-instrumentation-google-generativeai/opentelemetry/instrumentation/google_generativeai/span_utils.py
  • packages/opentelemetry-instrumentation-google-generativeai/tests/test_input_contents.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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.

🐛 Bug Report: Google GenAI instrumentation records no gen_ai.input.messages when contents is a tuple

1 participant