Skip to content

fix(vertexai): stop format_content crashing on string prompts and SDK Part objects - #1902

Merged
AmanAgarwal041 merged 3 commits into
openlit:mainfrom
Fahmid-Arman:fix/vertexai-string-contents
Nov 10, 2026
Merged

AmanAgarwal041 merged 3 commits into
openlit:mainfrom
Fahmid-Arman:fix/vertexai-string-contents

Conversation

@Fahmid-Arman

@Fahmid-Arman Fahmid-Arman commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Important

  1. We strictly follow a issue-first approach, please first open an issue relating to this Pull Request.
  2. PR title follows Conventional Commit format, for example feat: ... or
    fix(client): ....

Issue number: #1901

Change description:

format_content assumed every item in contents has .role and .parts, and that every part has all nine fields it reads. A string prompt (generate_content("...")) raised 'str' object has no attribute 'role', and vertexai.generative_models.Part raised 'Part' object has no attribute 'video_metadata'. Every streaming call and every non-streaming call with contents= by keyword ended with an ERROR span and no tokens, cost or input messages, although the request itself succeeded.

format_content and build_input_messages now accept a single string or Content in place of a list and treat string items as user text, and format_content reads each part field with getattr(part, field, None). As the SDK does, consecutive strings become the parts of one user message.

Tests: tests/test_vertexai_string_contents.py covers a string prompt, a list of strings, a single Content, a Content whose part has only text, and a full stream=True call with a string prompt. All three tests fail on main and pass with the change; the other Vertex AI tests still pass (23 in total). I also ran the same streaming call with real vertexai.generative_models objects (google-cloud-aiplatform 2.7.0): both the string prompt and the Content list now give an OK span with tokens and the input message. pylint is clean on both files.

Checklist

If your change doesn't seem to apply, please leave them unchecked.

  • PR title follows Conventional Commit format.
  • I have reviewed the contributing guidelines
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • I have performed a self-review of this change
  • Changes have been tested
  • Changes are documented

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the project license.

Summary by Sourcery

Make Vertex AI content handling robust across supported prompt and SDK object representations.

Bug Fixes:

  • Prevent Vertex AI content formatting and input-message extraction from failing for string prompts, single Content objects, and SDK Part objects with partial fields.
  • Preserve successful Vertex AI telemetry, including input messages and token usage, for streaming and non-streaming calls using supported content forms.

Enhancements:

  • Align consecutive string prompts with the Vertex AI SDK by recording them as parts of a single user message.

Tests:

  • Add coverage for string prompts, lists of strings, single Content objects, partial Part objects, and streaming telemetry.

@sourcery-ai

sourcery-ai Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Implementation

Handle string prompts, single Content objects, and Part objects with missing fields when formatting Vertex AI input and building telemetry messages.

  • sdk/python/src/openlit/instrumentation/vertexai/utils.py L36-64, L78-90, L124-125

Tests

Test string and Content input handling, partial Part objects, and successful streaming telemetry for a string prompt.

  • sdk/python/tests/test_vertexai_string_contents.py L1-131

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot 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.

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="sdk/python/src/openlit/instrumentation/vertexai/utils.py" line_range="36" />
<code_context>

     if not contents:
         return ""
+    if isinstance(contents, str):
+        contents = [contents]

</code_context>
<issue_to_address>
**Single content input raises**

When the caller passes a single SDK `Content` or `Part` directly as `contents`, `format_content` iterates the non-iterable object and raises `TypeError`, marking a successful request's span as an error; `build_input_messages` has the same assumption if reached independently.

Normalize a single `Content` or `Part` into a contents sequence before formatting or building input messages.
</issue_to_address>

### Comment 2
<location path="sdk/python/src/openlit/instrumentation/vertexai/utils.py" line_range="81-85" />
<code_context>
     for content in contents:
-        role = content.role
-        parts = content.parts
+        if isinstance(content, str):
+            formatted_messages.append(f"user: {content}")
+            continue
+        role = getattr(content, "role", "user")
         content_str = []
</code_context>
<issue_to_address>
**String parts become separate messages**

When a caller supplies a list of strings as Vertex AI contents, `build_input_messages` emits one user message for each string, and `format_content` likewise formats each as a separate user turn. Vertex AI treats a list of strings as multiple parts of one user content, so the recorded input messages misrepresent the request's message and part structure.

Group the strings into one user message with one text part per string, matching Vertex AI's interpretation of the list.

Also at `sdk/python/src/openlit/instrumentation/vertexai/utils.py:42-43`.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 2 findings to address first, and with message-content capture enabled, a formatting mistake could write incorrect prompt content to exported spans; reverting stops future spans but does not remove records already emitted. The impact is bounded to telemetry rather than changing application data or decisions.

Blocking findings: sdk/python/src/openlit/instrumentation/vertexai/utils.py:36, sdk/python/src/openlit/instrumentation/vertexai/utils.py:85


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@AmanAgarwal041
AmanAgarwal041 merged commit 4902959 into openlit:main Nov 10, 2026
7 checks passed
@github-actions

Copy link
Copy Markdown

Merged PR summary

  • Python SDK · Fixes — Fixes Vertex AI content formatting so string prompts, single Content objects, and SDK Part objects no longer crash, preserving telemetry and token usage.

Generated from the merged PR description and bounded, component-scoped file evidence. Release preparation reuses this validated summary.

This branch was previously deployed

1 inactive deployment
release-notes — fb9e86b9 Deployed Nov 10, 2026 by AmanAgarwal041 via Summarize / Merged pull request #272
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants