Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Jan 28, 2026

Related GitHub Issue

Closes: #11040

Description

This PR attempts to address Issue #11040 where some models (e.g., GLM 4.7 via OpenAI Compatible API) incorrectly pass the content parameter as a JSON object instead of a string when calling the write_to_file tool, causing processing to break silently.

Key implementation details:

  • Added a new coerceContentToString() helper method in NativeToolCallParser.ts that handles type coercion gracefully
  • Updated both parseToolCall() and createPartialToolUse() methods to use this helper for the write_to_file tool
  • Added explicit warning logging when coercion occurs so users are not left in the dark about model misbehavior
  • Follows the existing pattern for type coercion (e.g., coerceOptionalBoolean for booleans)

Feedback and guidance are welcome.

Test Procedure

  1. Unit tests added: New test cases in NativeToolCallParser.spec.ts covering:

    • Normal string content (unchanged behavior)
    • Object content coerced to JSON string
    • Array content coerced to JSON string
    • Number content coerced to string
    • Streaming and finalization paths
  2. Run tests:

    cd src && npx vitest run core/assistant-message/__tests__/NativeToolCallParser.spec.ts
  3. Manual verification: With a model like GLM 4.7 that sends object content, the tool should now work instead of breaking silently, and a warning will be logged.

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Testing: New and/or updated tests have been added to cover my changes (if applicable).
  • Documentation Impact: I have considered if my changes require documentation updates (see "Documentation Updates" section below).
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Documentation Updates

  • No documentation updates are required.

Additional Notes

This is a defensive fix that gracefully handles model misbehavior without changing the expected interface. Models that correctly output strings are unaffected.


Important

Adds content coercion to string for write_to_file tool in NativeToolCallParser.ts, with tests for various content types.

  • Behavior:
    • Adds coerceContentToString() in NativeToolCallParser.ts to convert non-string content to JSON string for write_to_file tool.
    • Updates parseToolCall() and createPartialToolUse() in NativeToolCallParser.ts to use coerceContentToString().
    • Logs a warning when content coercion occurs.
  • Tests:
    • Adds tests in NativeToolCallParser.spec.ts for write_to_file tool to verify content coercion from object, array, and number to string.
    • Tests cover normal, streaming, and finalization paths.

This description was created by Ellipsis for 09e8f31. You can customize this summary. It will automatically update as commits are pushed.

Some models (e.g., GLM 4.7 via OpenAI Compatible API) incorrectly pass
the content parameter as a JSON object instead of a string when calling
the write_to_file tool. This causes processing to break silently.

This fix adds defensive type coercion in NativeToolCallParser to:
1. Detect when content is not a string
2. Convert it to a formatted JSON string using JSON.stringify
3. Log a clear warning message so users understand what happened

This follows the existing pattern for type coercion (e.g.,
coerceOptionalBoolean for booleans).

Fixes #11040
@roomote
Copy link
Contributor Author

roomote bot commented Jan 28, 2026

Rooviewer Clock   See task on Roo Cloud

Review completed. No issues found.

The implementation is clean and well-structured:

  • Adds a coerceContentToString helper following the existing pattern of coerceOptionalBoolean
  • Correctly handles string, object, array, and number content types
  • Logs appropriate warnings when coercion occurs
  • All 13 tests pass with comprehensive coverage

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Status: Triage

Development

Successfully merging this pull request may close these issues.

[BUG] write_to_file breaks processing with unescaped JSON contents

1 participant