fix(aws): unwrap doubly-encoded JSON tool arguments from Nova Sonic#5411
Merged
theomonnom merged 3 commits intolivekit:mainfrom Apr 11, 2026
Merged
Conversation
07fdd4d to
81a7a91
Compare
Nova Sonic 2 may deliver toolUse.content as a doubly-encoded JSON string —
a JSON string whose parsed value is itself another JSON string. This caused
prepare_function_arguments to receive a str instead of a dict, crashing with:
TypeError: string indices must be integers, not 'str' (utils.py:404)
Fix: in _handle_tool_output_content_event, peel off one encoding layer when
the decoded value is still a string, so FunctionCall.arguments always holds
a proper single-encoded JSON object string.
Adds regression tests covering the double-encoded case, the normal case,
invalid JSON (no crash), and metadata forwarding.
…ject Tighten the unwrap guard so that legitimate string-valued raw tool schemas (e.g. content="hello") are preserved untouched. Previously any JSON string literal was unwrapped, which would rewrite "hello" -> hello (no longer valid JSON) and break from_json() downstream. Now the second layer is only peeled when json.loads(parsed) returns a dict, i.e. the inner string is itself a JSON object. Adds test_string_primitive_schema_not_unwrapped to cover this regression.
99a8ab1 to
0a13380
Compare
…_args.py Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
theomonnom
approved these changes
Apr 11, 2026
russellmartin-livekit
pushed a commit
that referenced
this pull request
Apr 13, 2026
osimhi213
added a commit
to de-id/livekit-agents
that referenced
this pull request
Apr 15, 2026
* upstream/main: (31 commits) chore: reduce renovate noise (livekit#5421) Rename e2ee to encryption in JobContext.connect (livekit#5454) feat: add Runway Characters avatar plugin (livekit#5355) Fix FrameProcessor lifecycle for selector based noise cancellation (livekit#5433) Feature - Configurable session close transcript timeout (livekit#5328) add ToolSearchToolset and ToolProxyToolset for dynamic tool discovery (livekit#5140) feat(beta/workflows): add InstructionParts for modular instruction customization (livekit#5077) fix: allow multiple AsyncToolsets by deduplicating management tools (livekit#5369) fix: empty transcript blocks commit_user_turn until timeout (livekit#5429) Feature/krisp viva sdk support (livekit#4370) feat: add service_tier parameter to Responses API LLM (livekit#5346) fix(inworld): do not leak connections when when cancelled (livekit#5427) update: Sarvam STT - add verbose error loggin and remove retry connection (livekit#5373) chore(deps): update github workflows (major) (livekit#5424) (azure openai): ensure gpt-realtime-1.5 compatibility (livekit#5407) chore(deps): update dependency nltk to v3.9.4 [security] (livekit#5418) chore(deps): update dependency aiohttp to v3.13.4 [security] (livekit#5416) chore(deps): update dependency langchain-core to v1.2.28 [security] (livekit#5417) chore: pin GHA by commit (livekit#5415) fix(aws): unwrap doubly-encoded JSON tool arguments from Nova Sonic (livekit#5411) ...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Nova Sonic 2 (AWS Bedrock) occasionally delivers
toolUse.contentas a doubly-encoded JSON string — a JSON string whose parsed value is itself another JSON string containing a JSON object. This causedprepare_function_argumentsto receive astrinstead of adict, crashing with:Root cause
In
_handle_tool_output_content_event,argsis passed directly toFunctionCall(arguments=args). When Nova Sonic double-encodes the arguments,pydantic_core.from_json()downstream returns a Pythonstrinstead of adict, then indexing with a param name string fails.Fix
Peel off one encoding layer when the outer JSON parse yields a string — but only if that inner string is itself a valid JSON object (
dict). This is the critical guard:Double-encoded object (bug case):
"\"{ \\\"date\\\": \\\"2026\\\" }\""→ outer parse →
"{ \"date\": \"2026\" }"(str)→ inner parse →
{"date": "2026"}(dict ✓) → peel: args becomes the inner JSON string ✓Plain JSON object (normal case):
"{\"date\": \"2026\"}"→ outer parse →
{"date": "2026"}(dict, not str) → no peel ✓Legitimate string primitive (raw schema, e.g.
content="hello"):"\"hello\""→ outer parse →
"hello"(str)→ inner parse →
JSONDecodeError(not valid JSON object) → no peel: args stays"\"hello\""✓(Without this guard, args would be rewritten to bare
hello— invalid JSON — breakingfrom_json()downstream.)Changes
livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realtime/realtime_model.py— unwrap guard in_handle_tool_output_content_eventlivekit-plugins/livekit-plugins-aws/tests/test_nova_sonic_tool_args.py— 5 regression testsTest plan
test_doubly_encoded_string_is_unwrapped— Nova Sonic bug case fixedtest_single_encoded_string_passed_through— normal JSON object args unaffectedtest_invalid_json_string_does_not_crash— invalid JSON left as-is, no crashtest_string_primitive_schema_not_unwrapped—"hello"stays'"hello"', not rewritten to barehellotest_tool_name_and_id_forwarded_correctly— metadata forwarded correctly