Do not execute a tool from partial streamed JSON

Do not execute a tool from partial streamed JSON

A streamed tool call can look ready before it is safe. Buffer deltas per call; wait for complete JSON, parse strictly, validate schema and business constraints, then check authorization before side effects. Parseable is not permission.

The v1.5.0 Anthropic Python SDK release keeps partial tool input JSON off content blocks. Treat that as an application boundary, not proof that another provider emits identical events. If the stream ends mid-document, retain it for diagnosis and do not execute it.

Keep execution state outside the stream: call identity, validated arguments, and not_started, running, or completed. Make running atomic with the tool’s deduplication boundary so reconnects and duplicate terminal events cannot repeat actions.

Fallback must preserve the boundary. The error guidance says authentication and path errors require repair; bounded backoff is for classified transient 429 or 5xx failures. A 500 with invalid_request is still a payload bug. Before switching models, verify the next route accepts the same contract.

Never replay blindly after interruption: consult the execution record, resume from a checkpoint, or stop with controlled uncertainty. Test truncated and invalid JSON, disconnects before and after execution, duplicate events, and each error class. The pass condition is at-most-once side effects—or an auditable stop—not merely a successful fallback.

The recovery runbook needs one more distinction: a local running record is not proof of what happened at a remote tool. Consider a fully validated call whose side effect commits, followed by a worker crash before completed is saved. Marking execution atomically in the application database does not, by itself, close that remote commit gap.

I would make recovery conditional on the tool’s contract:

  • If it supports durable idempotency, persist an application-owned operation key before dispatch and reuse it across reconnects or model fallback. Check the tool’s key scope and retention window before treating a replay as safe; a fresh model-generated call ID should not create a fresh business operation.
  • If it offers authoritative status lookup, reconcile that operation before resubmitting. A lookup timeout remains unknown, not evidence of failure.
  • If neither mechanism exists, stop automatic execution on an ambiguous outcome and require reconciliation. Expiring a worker lease is not permission to repeat the side effect.

For a fault-injection test, kill the worker after the remote commit but before the local completion update. Require recovery to produce either a verified result for the original operation or an explicit unresolved outcome with an escalation owner—not a second dispatch merely because the stream can be regenerated. This tests a different boundary from partial-JSON validation: whether the execution record can actually support the promised recovery policy.