Skip to content

Handle stringified JSON tool call arguments#172

Open
kuosuko wants to merge 1 commit intoml-explore:mainfrom
kuosuko:fix-json-toolcall-stringified-arguments
Open

Handle stringified JSON tool call arguments#172
kuosuko wants to merge 1 commit intoml-explore:mainfrom
kuosuko:fix-json-toolcall-stringified-arguments

Conversation

@kuosuko
Copy link
Copy Markdown

@kuosuko kuosuko commented Mar 30, 2026

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
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)

2 participants