Skip to content

Give the model a useful nudge when a tool call is truncated/unparseable, instead of a bare JSON parse error #414

Closed

Description

@agenteinstinct1-max

Problem

When a model's tool call is truncated mid-stream (typically because the response hits the output-token limit while generating a large tool-call payload, e.g. writing a whole file in one write call), the harness surfaces only the raw JSON parse error to the model:

The arguments provided to the tool are invalid: JSON Parse error: Unterminated string

The model gets no hint about why the arguments were unparseable or how to recover, so it tends to improvise: splitting files into arbitrary chunks, concatenating via shell commands, or retrying the same oversized call. All of these waste turns and can produce worse output than a guided retry.

How it works today

  • experimental_repairToolCall in packages/opencode/src/session/llm.ts handles the AI SDK's failed tool calls. Known-name fixes (case mismatch, deferred tool discovery) get targeted repairs; everything else falls through to a generic fallback.
  • The generic fallback rewrites the call to the invalid tool with { tool, error: failed.error.message }.
  • packages/opencode/src/tool/invalid.ts returns that message verbatim to the model as the tool output.

So the only feedback the model receives for a truncated payload is the underlying parser's error text.

Proposal

Keep the repair flow as-is, but enrich the error text passed to the invalid tool when the failure looks like a JSON syntax/parse failure (e.g. the AI SDK's InvalidToolInputError caused by a JSON parse error, or messages like Unterminated string / unexpected end of input):

  • Name the likely cause: the tool-call arguments were cut off before they were complete, most commonly because the response reached the output-token limit for a single message.
  • Give concrete recovery guidance: retry with smaller arguments; when writing large files, split the content across several smaller tool calls or append in chunks instead of one large payload; keep each individual tool call comfortably within the model's output budget.

This should stay a nudge: the model remains free to retry any shape of call. No hard blocks, no changes to tool schemas, and no changes to effort/system prompts.

Optional refinement, if the AI SDK version in use exposes it: cross-check the step finish reason (length) to raise confidence that the parse failure was a truncation before adding the limit-specific wording.

Acceptance criteria

  • A tool call whose arguments fail JSON parsing produces invalid-tool output that explains the truncation hypothesis and how to retry with smaller payloads, not just the raw parse error.
  • Non-parse failures (schema validation, unknown tools) keep their current wording.
  • No behavioral restriction: the model can still retry the identical call if it chooses.

Activity

  1. agenteinstinct1-max commented on Oct 21, 2026

    @agenteinstinct1-max
    CollaboratorAuthor

    Fixed by #437 (merged).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions