Skip to content

fix(aws): unwrap doubly-encoded JSON tool arguments from Nova Sonic#5411

Merged
theomonnom merged 3 commits intolivekit:mainfrom
rililinx:fix/nova-sonic-tool-args-json-parse
Apr 11, 2026
Merged

fix(aws): unwrap doubly-encoded JSON tool arguments from Nova Sonic#5411
theomonnom merged 3 commits intolivekit:mainfrom
rililinx:fix/nova-sonic-tool-args-json-parse

Conversation

@rililinx
Copy link
Copy Markdown
Contributor

@rililinx rililinx commented Apr 10, 2026

Summary

Nova Sonic 2 (AWS Bedrock) occasionally delivers toolUse.content as a doubly-encoded JSON string — a JSON string whose parsed value is itself another JSON string containing a JSON object. This caused prepare_function_arguments to receive a str instead of a dict, crashing with:

TypeError: string indices must be integers, not 'str'  (livekit/agents/llm/utils.py:404)

Root cause

In _handle_tool_output_content_event, args is passed directly to FunctionCall(arguments=args). When Nova Sonic double-encodes the arguments, pydantic_core.from_json() downstream returns a Python str instead of a dict, 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 — breaking from_json() downstream.)

if isinstance(args, str):
    try:
        parsed = json.loads(args)
        if isinstance(parsed, str):
            try:
                inner = json.loads(parsed)
                if isinstance(inner, dict):
                    args = parsed  # peel one layer of double-encoding
            except (json.JSONDecodeError, TypeError):
                pass  # inner string is a plain value, leave args untouched
    except (json.JSONDecodeError, TypeError):
        pass

Changes

  • livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realtime/realtime_model.py — unwrap guard in _handle_tool_output_content_event
  • livekit-plugins/livekit-plugins-aws/tests/test_nova_sonic_tool_args.py — 5 regression tests

Test plan

  • test_doubly_encoded_string_is_unwrapped — Nova Sonic bug case fixed
  • test_single_encoded_string_passed_through — normal JSON object args unaffected
  • test_invalid_json_string_does_not_crash — invalid JSON left as-is, no crash
  • test_string_primitive_schema_not_unwrapped"hello" stays '"hello"', not rewritten to bare hello
  • test_tool_name_and_id_forwarded_correctly — metadata forwarded correctly

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 10, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

@rililinx rililinx force-pushed the fix/nova-sonic-tool-args-json-parse branch from 07fdd4d to 81a7a91 Compare April 10, 2026 17:46
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.
@rililinx rililinx force-pushed the fix/nova-sonic-tool-args-json-parse branch from 99a8ab1 to 0a13380 Compare April 10, 2026 17:49
devin-ai-integration[bot]

This comment was marked as resolved.

…_args.py

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@theomonnom theomonnom merged commit 816de49 into livekit:main Apr 11, 2026
15 checks passed
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)
  ...
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.

3 participants