Skip to content

Comments

Tool content validation#806

Merged
willccbb merged 2 commits intomainfrom
cursor/tool-content-validation-8916
Jan 30, 2026
Merged

Tool content validation#806
willccbb merged 2 commits intomainfrom
cursor/tool-content-validation-8916

Conversation

@willccbb
Copy link
Member

@willccbb willccbb commented Jan 30, 2026

Description

Tightens the content validation in ToolEnv.call_tool to ensure that tool return values are only preserved as lists if they conform to the text or image_url content 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 text and image_url content while ensuring other unexpected list formats are serialized.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Test improvement

Testing

  • All existing tests pass when running uv run pytest locally.
  • New tests have been added to cover the changes

Checklist

  • My code follows the style guidelines of this project as outlined in AGENTS.md
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

Additional Notes

While the OpenAI SDK (v2.16.0) officially supports only str | Iterable[ChatCompletionContentPartTextParam] for ChatCompletionToolMessageParam content, this implementation includes image_url support 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.


Open in Cursor Open in Web


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 with type: "text" or type: "image_url"; all other list-shaped returns are coerced to str.

Adds is_valid_tool_content_parts (with a shared VALID_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.

… 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
Copy link

cursor bot commented Jan 30, 2026

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Co-authored-by: williambrown97 <williambrown97@gmail.com>
@willccbb willccbb marked this pull request as ready for review January 30, 2026 17:24
@willccbb willccbb merged commit 37c373d into main Jan 30, 2026
6 checks passed
@willccbb willccbb deleted the cursor/tool-content-validation-8916 branch January 30, 2026 17:32
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