Skip to content

[NVBug 6899706] Handle malformed tool-call JSON in /v1/messages and flag failed Claude Code runs #4529

Open

Description

@qiaochuz-nv

Queue rank 40/48 · Difficulty 3/5 · Status on main: not_fixed

NVBug: https://nvbugspro.nvidia.com/bug/6899706 · NVBug priority: Unprioritized · ARB: Ananth Subramaniam
GitHub: none found (open but non-fixing: #3509 orjson swap, #4062 sandboxed agent, #4015 transcript/timeout; also #2507/#4027 related)

Summary

Two linked defects on the Claude Code path. Part 1: the default /v1/messages adapter runs a bare json.loads on model tool-call arguments; malformed JSON raises JSONDecodeError, which the generic middleware turns into HTTP 500. Claude Code treats 500 as transient and resends; when retries exhaust, the run dies. Part 2: claude_code_agent saves that failed CLI run as a completed, unflagged reward-0 rollout, so an infra failure is counted as a valid measurement.

Reproduction

Offline, deterministic (Part 1): run the bug's repro_converter.py with the checkout interpreter — AnthropicConverter().responses_to_anthropic_response(resp) on a function_call with unescaped quotes raises JSONDecodeError. E2E (both parts, intermittent; depends on policy output): gym env start --config environments/claude_code_math/config.yaml ... then gym eval run --agent claude_code_math_agent ...; a task pads to reward 0, finished_naturally true.

Root cause

Part 1: nemo_gym/anthropic_converter.py:745-749 _json_object_from_arguments does json.loads(arguments) with no except; messages() converts before streaming (base_responses_api_model.py:514), so the error is a plain 500 via server_utils.py:1364 JSONResponse(content=repr(e), status_code=500). Part 2: _invocation_outcome computes "failed" (claude_code_agent/app.py:519) but _create_response (637-653) returns a NeMoGymResponse with no status/error; run() (765) only adds turns_used/finished_naturally, never failure_kind/failure_reason/mask_sample.

Status on main (not_fixed)

Converter byte-identical to bug revision: git log efe58abe..HEAD -- nemo_gym/anthropic_converter.py is empty; lines 745-749 unchanged. grep failure_kind|mask_sample|agent_run_error responses_api_agents/claude_code_agent/ returns nothing. Middleware still returns repr(e)/500. All three cited PRs still OPEN and none fix this.

Proposed fix

Part 1 (anthropic_converter.py): catch JSONDecodeError/non-object in the response direction, mirroring simple_agent (#1180) — surface a correctable failed tool_call or a typed non-5xx the client won't blindly resend; log truncated raw args. Part 2 (claude_code_agent/app.py): carry _invocation_outcome into response.status/error; in run() set failure_kind=agent_run_error (vocab exists, failure_kinds.py:72), failure_reason, finished_naturally=false, and mask_sample per base_resources_server.py contract. Add unit tests: unescaped-quote command string; failed CLI run flagged.

Validation

testcases/environments/claude_code_math.py (assertions at 239, 262, 269, 271). Re-run the README-stack QA case; expect line 269 (output_tokens > 0) to pass once the row is a real rollout or is correctly flagged/masked.

Triage

Difficulty 3/5 — two coupled components, new unit tests, and a maintainer design call on mask_sample semantics. Effort ~8 h. Needs GPU/model: no (Part 1 unit-testable offline; hosted endpoint for optional smoke). External dependency: none.

Reviewer notes

Verifier confirmed not_fixed and difficulty 3 (high confidence); all cited code line numbers and QA assertion lines (239/262/269/271) verified. Correction applied: the E2E reproduction is flagged intermittent (padded failure in 2 of 4 QA runs; exact task-3 attribution unprovable). Added #2507/#4027 as further related non-fixing PRs.

Activity

  1. chris320211 commented on Oct 15, 2026

    @chris320211

    Hi @qiaochuz-nv, I would like to pick up if this issue if that is ok. Thank you.

  2. yuhezhang-ai commented on Oct 21, 2026

    @yuhezhang-ai
    Contributor

    Cross-linking the failed-Claude-Code-run reporting portion of this issue with #3180, the failure-reporting migration tracker now assigned to @yuhezhang-ai. This issue remains under #4482; the malformed-tool-call conversion work stays here as well.

    #4882 covers diagnostic-only fixes to already-masked results. Correctly classifying and flagging the currently unflagged failed runs described here can change result semantics, so keep that implementation and its tests in this issue and identify any required caller support from #2135 / #3302.

  3. xiao98 commented on Dec 1, 2026

    @xiao98

    Hi @chris320211, are you still working on this? If not, I'd like to pick up Part 1 (the /v1/messages converter). Part 2 is already tracked in #3180, so I'll leave it alone.

    My plan for Part 1: in AnthropicConverter._json_object_from_arguments, catch JSONDecodeError and non-object arguments instead of letting them become an HTTP 500. I see two options:

    1. Return a tool_use block with input: {} and log the truncated raw arguments. Claude Code then rejects the call through its own input validation, and the model gets an error it can correct. This is the closest to what simple_agent does.
    2. Return a non-5xx error so the client doesn't retry the same bad output.

    I lean toward option 1 and would add a unit test with the unescaped-quote case from the bug. @qiaochuz-nv, does that direction work for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions