Repository navigation
fix(vertexai): stop format_content crashing on string prompts and SDK Part objects - #1902
Conversation
Reviewer's GuideImplementationHandle string prompts, single Content objects, and Part objects with missing fields when formatting Vertex AI input and building telemetry messages.
TestsTest string and Content input handling, partial Part objects, and successful streaming telemetry for a string prompt.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
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
Merged PR summary
Generated from the merged PR description and bounded, component-scoped file evidence. Release preparation reuses this validated summary. |
Important
feat: ...orfix(client): ....Issue number: #1901
Change description:
format_contentassumed every item incontentshas.roleand.parts, and that every part has all nine fields it reads. A string prompt (generate_content("...")) raised'str' object has no attribute 'role', andvertexai.generative_models.Partraised'Part' object has no attribute 'video_metadata'. Every streaming call and every non-streaming call withcontents=by keyword ended with an ERROR span and no tokens, cost or input messages, although the request itself succeeded.format_contentandbuild_input_messagesnow accept a single string orContentin place of a list and treat string items as user text, andformat_contentreads each part field withgetattr(part, field, None). As the SDK does, consecutive strings become the parts of one user message.Tests:
tests/test_vertexai_string_contents.pycovers a string prompt, a list of strings, a singleContent, aContentwhose part has onlytext, and a fullstream=Truecall with a string prompt. All three tests fail onmainand pass with the change; the other Vertex AI tests still pass (23 in total). I also ran the same streaming call with realvertexai.generative_modelsobjects (google-cloud-aiplatform 2.7.0): both the string prompt and theContentlist now give an OK span with tokens and the input message.pylintis clean on both files.Checklist
If your change doesn't seem to apply, please leave them unchecked.
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:
Enhancements:
Tests: