Skip to content

fix: sanitize control characters in ToolCall::arguments() before json_decode#937

Open
ruttydm wants to merge 1 commit intoprism-php:mainfrom
ruttydm:fix/sanitize-toolcall-control-characters
Open

fix: sanitize control characters in ToolCall::arguments() before json_decode#937
ruttydm wants to merge 1 commit intoprism-php:mainfrom
ruttydm:fix/sanitize-toolcall-control-characters

Conversation

@ruttydm
Copy link

@ruttydm ruttydm commented Mar 2, 2026

Summary

  • Sanitize raw control characters (0x00-0x1F, 0x7F) from tool call argument strings before json_decode in ToolCall::arguments()
  • These bytes are invalid in JSON per RFC 8259 and cause JsonException: Control character error, possibly incorrectly encoded
  • Observed 11 times in production with the DeepSeek streaming provider

Context

Some providers (notably DeepSeek) occasionally include raw control characters in streamed tool call argument JSON. The OpenRouter handler already has defensive handling for malformed arguments — this adds similar protection at the ToolCall level so all providers benefit.

Fixes #936

…_decode

Some providers (e.g. DeepSeek) may include raw control characters
(0x00-0x1F, 0x7F) in streamed tool call argument JSON. These bytes are
invalid per RFC 8259 and cause json_decode to throw a JsonException with
"Control character error, possibly incorrectly encoded".

Strip them before decoding. This mirrors the defensive approach already
used in the OpenRouter streaming handler.

Fixes prism-php#936
Copy link
Contributor

@sixlive sixlive left a comment

Choose a reason for hiding this comment

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

Can you address CI errors please

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.

JsonException: Control character error in ToolCall::arguments() with DeepSeek streaming

2 participants