fix: coerce write_to_file content to string when model sends object #11041
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.
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
contentparameter as a JSON object instead of a string when calling thewrite_to_filetool, causing processing to break silently.Key implementation details:
coerceContentToString()helper method inNativeToolCallParser.tsthat handles type coercion gracefullyparseToolCall()andcreatePartialToolUse()methods to use this helper for thewrite_to_filetoolcoerceOptionalBooleanfor booleans)Feedback and guidance are welcome.
Test Procedure
Unit tests added: New test cases in
NativeToolCallParser.spec.tscovering:Run tests:
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
Documentation Updates
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_filetool inNativeToolCallParser.ts, with tests for various content types.coerceContentToString()inNativeToolCallParser.tsto convert non-string content to JSON string forwrite_to_filetool.parseToolCall()andcreatePartialToolUse()inNativeToolCallParser.tsto usecoerceContentToString().NativeToolCallParser.spec.tsforwrite_to_filetool to verify content coercion from object, array, and number to string.This description was created by
for 09e8f31. You can customize this summary. It will automatically update as commits are pushed.