Merged
Conversation
… parts - Add _is_valid_tool_content_parts() function to validate tool return content - Valid content parts must have 'type' field with value 'text' or 'image_url' - Lists that don't contain valid content parts are now cast to str - Add comprehensive tests for the validation logic Co-authored-by: williambrown97 <williambrown97@gmail.com>
|
Cursor Agent can help with this pull request. Just |
|
|
Co-authored-by: williambrown97 <williambrown97@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Tightens the content validation in
ToolEnv.call_toolto ensure that tool return values are only preserved as lists if they conform to thetextorimage_urlcontent part format. Any other list types or invalid content part structures are now cast to a string.This change aligns tool output handling with expected message formats, allowing for structured
textandimage_urlcontent while ensuring other unexpected list formats are serialized.Type of Change
Testing
uv run pytestlocally.Checklist
Additional Notes
While the OpenAI SDK (v2.16.0) officially supports only
str | Iterable[ChatCompletionContentPartTextParam]forChatCompletionToolMessageParamcontent, this implementation includesimage_urlsupport for tool returns to be forward-compatible with providers that may accept this format, and to align with potential practical use cases as suggested by the task.Note
Low Risk
Small, well-tested validation change limited to how tool return values are serialized into tool messages; main risk is behavior change for tools that previously returned arbitrary lists.
Overview
Tightens tool output handling in
ToolEnv.call_tool. Tool return values are now preserved as a list only when they validate as structured content parts withtype: "text"ortype: "image_url"; all other list-shaped returns are coerced tostr.Adds
is_valid_tool_content_parts(with a sharedVALID_TOOL_CONTENT_PART_TYPES) and expands tests to cover valid/mixed content parts as well as invalid list shapes that should be stringified.Written by Cursor Bugbot for commit 54e997f. This will update automatically on new commits. Configure here.