Keep non-object tool arguments invalid in `BaseToolCallPart.args_as_dict` under `python -O`

Merged
DS
22d ago
+41-1

This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.

Closes #10525

Why we make these changes

BaseToolCallPart.args_as_dict rejected valid JSON that isn't an object ([], "text", 1, null) with an assert. python -O strips asserts, so under -O the method returned the raw value despite its dict[str, Any] return type, args_as_json_str passed it on unwrapped, and raise_if_invalid=True did not raise. The check is now an explicit if that raises the same AssertionError, which the docstring documents and callers already catch.

The issue's LoadCapabilityCallPart.capability_id crash no longer reproduces on main; #9710 removed it. The remaining -O crash, AttributeError: 'list' object has no attribute 'get' in _load_capability_ids_by_call (reached through Model._translate_legacy_tool_reveals), goes away with this fix.

Thanks to @AryanRG (#10491) and @devtyagi3909 (#10626), whose closed PRs proposed this fix.

New public surface

none

User-visible behavior

Under python -O:

part = ToolCallPart(tool_name='t', args='[]')
part.args_as_dict()
#> before: []
#> after:  {'INVALID_JSON': '[]'}
part.args_as_dict(raise_if_invalid=True)
#> before: []
#> after:  AssertionError: args should be a dict

Verification

What changes for existing users

Under python -O, non-object JSON tool arguments get the same INVALID_JSON handling as without it. Nothing changes without -O.

PR decisions

2026-11-12 · keep AssertionError for non-object JSON · iter -

  • Decision: raise_if_invalid=True keeps raising AssertionError for non-object JSON; not switched to ValueError
  • Why: args_as_dict docstring names it, test_args_as_dict_raise_if_invalid_non_dict_json pins it, callers catch (AssertionError, ValueError); switching breaks them (manager-relayed blind validator)
  • Source: https://github.com/pydantic/pydantic-ai/issues/10525#issuecomment-6226438681
  • Session: 9409d67c-c42d-47ad-88d2-2a50265fb9ff

2026-11-12 · headline capability_id crash already gone on main · iter -

  • Decision: do not test LoadCapabilityCallPart.capability_id; test args_as_dict itself
  • Why: capability_id under -O returns None on main 4ece65be2 since #9710; the remaining -O crash is models/init.py::_load_capability_ids_by_call (AttributeError on list.get)
  • Source: https://github.com/pydantic/pydantic-ai/pull/9710
  • Session: 9409d67c-c42d-47ad-88d2-2a50265fb9ff

2026-11-12 · regression test runs in a subprocess under -O · iter -

  • Decision: one test launching sys.executable -O, marked @pytest.mark.subprocess(reason=...), COVERAGE_* scrubbed
  • Why: a strippable assert only differs under -O, which is fixed at interpreter start; tests/AGENTS.md Test cost allows the marker when the claim depends on the process boundary
  • Source: https://github.com/pydantic/pydantic-ai/issues/10525
  • Session: 9409d67c-c42d-47ad-88d2-2a50265fb9ff

2026-11-12 · other assert isinstance guards out of scope · iter -

  • Decision: "the other assert isinstance guards in messages.py were not reproduced under -O. If you reproduce a defect in one, report it ... do not fold it in."
  • Why: root AGENTS.md scope rule: extend only to reproduced siblings (manager-relayed)
  • Source: https://github.com/pydantic/pydantic-ai/issues/10525
  • Session: 9409d67c-c42d-47ad-88d2-2a50265fb9ff

2026-11-12 · pre-push review 1 of 3 at b20039402b4739804a3f7e0c9edb3566ba98e2f5 · iter -

2026-11-12 · pre-push review 1 of 3 at b20039402b4739804a3f7e0c9edb3566ba98e2f5 · iter -

2026-11-12 · douwebot label authorized for every head · iter -

  • Decision: "Apply the douwebot label once CI and PR Review Checks pass on the head ... you need not ask again on later heads of this PR. Do not enqueue the PR or re-enqueue it after an ejection: I do both."
  • Why: merge bar requires clean douwebot APPROVE for a non-trivial S; manager (relaying David) owns enqueue and re-enqueue
  • Source: https://github.com/pydantic/pydantic-ai/pull/13268
  • Session: 9409d67c-c42d-47ad-88d2-2a50265fb9ff

2026-11-12 · douwebot 429 retry rule · iter -

  • Decision: "when a douwebot run fails before reviewing with a provider 429 (the run log shows go-limit or rate-limit, status 429), re-apply the label once after about 15 minutes, using a background wait rather than a foreground sleep. If the retry also fails with 429, report to me and hold."
  • Why: 429s are intermittent across douwebot runs; manager (relaying David) ordered an immediate re-apply for run 43264620265 plus this standing rule
  • Source: https://github.com/pydantic/pydantic-ai/actions/runs/43264620265
  • Session: 9409d67c-c42d-47ad-88d2-2a50265fb9ff

2026-11-12 · douwebot retry rule widened to parse failures · iter -

  • Decision: "Your standing retry rule now covers both causes: a provider 429, and a fallback model's output failing to parse. Retry once per failure. If two runs in a row fail without a verdict, report to me and hold." Re-apply about 15 minutes after each failure, from a background wait.
  • Why: run 43266067725 failed on prose-before-JSON in ReviewResult validation; manager (relaying David) routes that douwebot pipeline bug separately (also hit #13269); count of consecutive failures restarts with this order
  • Source: https://github.com/pydantic/pydantic-ai/actions/runs/43266067725
  • Session: 9409d67c-c42d-47ad-88d2-2a50265fb9ff
  • Supersedes: douwebot 429 retry rule

2026-11-12 · GitHub polling interval 120s · iter -

  • Decision: "Relaunch every background watch you run (monitor mode, douwebot wait, CI wait) with sleep 120 instead of sleep 30 between polls, and keep that interval for the rest of this PR."
  • Why: shared GitHub REST quota for the account was exhausted until 17:40Z; polling was the main consumer
  • Source: https://github.com/pydantic/pydantic-ai/pull/13268
  • Session: 9409d67c-c42d-47ad-88d2-2a50265fb9ff

2026-11-12 · CI log download rule · iter -

  • Decision: "Download each CI job log at most once, save it to a file in your scratchpad, and read it from disk after that. Start with one failing-required-job-style lookup rather than repeated log pulls, and poll every 120s."
  • Why: the REST quota is shared by every dsfaccini gh call on every machine and hit 0 once today
  • Source: https://github.com/pydantic/pydantic-ai/pull/13268
  • Session: 9409d67c-c42d-47ad-88d2-2a50265fb9ff

2026-11-12 · douwebot retries paused · iter -

  • Decision: cancelled the 18:03Z douwebot retry; hold in monitor mode until the manager sends the go (parser fix merged or OpenCode Go recovered)
  • Why: every douwebot run fails on the prose-before-JSON parse bug while OpenCode Go returns 429s, and each retry burns provider quota (runs 43266067725, 43272057220)
  • Source: https://github.com/pydantic/pydantic-ai/actions/runs/43272057220
  • Session: 9409d67c-c42d-47ad-88d2-2a50265fb9ff
  • Supersedes: douwebot retry rule widened to parse failures

2026-11-12 · watchers use a horizon · iter -

  • Decision: monitor mode and the douwebot wait follow managed-pr-template.md blocks 2-3 (tooling 982e5be): horizon H, nap a sixth of the time left; nap floor kept at 120 s; monitor SINCE pinned at 2026-11-12T17:47:14Z on relaunch
  • Why: toolkit watchers dropped --interval/--timeout for horizons (manager, relaying David); the 120 s floor keeps the standing quota rule
  • Source: https://github.com/pydantic/pydantic-ai/pull/13268
  • Session: 9409d67c-c42d-47ad-88d2-2a50265fb9ff
  • Supersedes: GitHub polling interval 120s

2026-11-12 · douwebot go after parser fix · iter -

  • Decision: douwebot label re-applied on 5ae1428fe at 2026-11-12T19:37:42Z; one retry per transient pipeline failure (provider 429 or fallback parse failure) resumes; two consecutive failures without a verdict -> report and hold
  • Why: #13286 (douwebot parser) merged 2026-11-12T19:35:27Z as 1f631a3ea; manager (relaying David) sent the go
  • Source: https://github.com/pydantic/pydantic-ai/pull/13286
  • Session: 9409d67c-c42d-47ad-88d2-2a50265fb9ff
  • Supersedes: douwebot retries paused

2026-11-12 · merge bar met on 5ae1428fe · iter -

  • Decision: douwebot APPROVE (run 43292108952, receipt head=5ae1428fe, engine=opencode-zai), no douwebot inline comments, no review threads; CI green, PR Review Checks success; step-7 second-opinion choice sent to manager with recommendation none
  • Why: S, 2 files, +41/-1; no public API or docs change; behavior moves only under -O, back to the documented args_as_dict contract, already covered by douwebot, PR Review Checks and pre-push review
  • Source: https://github.com/pydantic/pydantic-ai/pull/13268#pullrequestreview-5880950002
  • Session: 9409d67c-c42d-47ad-88d2-2a50265fb9ff

2026-11-12 · step 7: no second opinion · iter -

  • Decision: no additional review surface after the merge bar (step 7 = none); final metadata check next, no head-moving change
  • Why: David via p2-mgr-1, 2026-11-12: picked none from the step-7 menu; recommendation was none (S, 2 files, no public API or docs change)
  • Source: https://github.com/pydantic/pydantic-ai/pull/13268
  • Session: 9409d67c-c42d-47ad-88d2-2a50265fb9ff

2026-11-12 · final metadata check · iter -

  • Decision: fresh Haiku checker from policy base 8ffe1d851: title and labels current; one kept correction applied: body PR-decisions mirror extended from 6 to all 16 local entries; head unchanged at 5ae1428fe
  • Why: pushing-commits-to-the-repo 'Publish PR decisions' requires the entries and 'the local log is authoritative'; no confidential reasoning in the added entries
  • Source: https://github.com/pydantic/pydantic-ai/pull/13268
  • Session: 9409d67c-c42d-47ad-88d2-2a50265fb9ff

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.

https://claude.ai/code/session_01HhQjZAdCrYC1qC8zrK535w


Summary by cubic

Fixes BaseToolCallPart.args_as_dict returning raw non-object JSON under python -O. The assert rejecting [], scalars, and null was stripped under -O, so those values returned raw instead of the INVALID_JSON dict; an explicit if raising the same AssertionError now makes raise_if_invalid=True and the fallback behave identically with and without -O. Closes #10525.

Adds a regression test that runs args_as_dict in a python -O subprocess for a list, a string, a number, and null. The issue's original capability_id crash no longer reproduces on main; this change fixes the remaining -O crash in _load_capability_ids_by_call.