Skip to content

Handle stringified JSON tool call arguments - #172

Merged
davidkoski merged 2 commits into
ml-explore:mainfrom
kuosuko:fix-json-toolcall-stringified-arguments
May 11, 2026
Merged

davidkoski merged 2 commits into
ml-explore:mainfrom
kuosuko:fix-json-toolcall-stringified-arguments

Conversation

@kuosuko

@kuosuko kuosuko commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #169.

Problem

JSONToolCallParser assumes arguments is always a JSON object, but some models emit it as a JSON-encoded string instead.

That means payloads like this fail to parse today:

{"name":"get_weather","arguments":"{\"location\":\"Paris\"}"}

As a result, Swift drops the tool call entirely.

Fix

Normalize the outer JSON payload before decoding ToolCall.Function:

  • decode the top-level object with JSONSerialization
  • if arguments is a string, parse that string as JSON
  • replace arguments with the parsed value
  • decode the normalized payload using the existing JSONDecoder

This preserves the current behavior for object-shaped arguments while adding compatibility for stringified arguments.

Testing

  • swift test --filter ToolTests
  • added regression tests for stringified object arguments
  • added regression tests for stringified empty arguments
  • added regression tests for stringified array arguments

I also verified locally with Granite tool calling that the original parser emitted no parsed tool call while the patched parser successfully emitted get_weather(location: "Tokyo", unit: "celsius").

@kuosuko
kuosuko marked this pull request as ready for review March 30, 2026 16:53
@davidkoski davidkoski added the swift-format Swift format failure in CI label Apr 9, 2026
JaeminKim-amoz

This comment was marked as spam.

@DePasqualeOrg

Copy link
Copy Markdown
Contributor

I've addressed the parsing problems in this library in Swift LM Response Parser, which parses reasoning and tool-use output from all models implemented in MLX Swift LM and emits a stream of Open Responses events. I created a demo of it in this PR in MLX Swift Examples and submitted a proposal for how it could be used with MLX Swift LM.

If you're able to test it for your use case, please post your feedback here or in an issue in the repo.

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you!

@davidkoski
davidkoski merged commit f5c7ff8 into ml-explore:main May 11, 2026
3 checks passed
davidtai pushed a commit to davidtai/mlx-swift-lm that referenced this pull request Oct 6, 2026
…ceipts (ml-explore#172)

* Add calibrated first-content admission and qualification timing

* Record bounded committed-token timing for qualification

* Apply required Swift formatting to changed SDK files

* Keep qualification receipts complete at observation limits

* Align calibrated admission clocks and early decode context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

swift-format Swift format failure in CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] JSONToolCallParser fails to parse stringified arguments (used by Granite 4 and others)

4 participants