Bound the work of `$ref` inlining, decision-model fields, `MessagesBuilder.add` and the `private_key` pattern

Merged
DS
1mo ago
+483-34

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

Why we make these changes

Four code paths now bound their work:

  • Inlining $defs. With prefer_inlined_defs, JsonSchemaTransformer now counts the JSON values (objects, arrays and scalars) in each inlined copy of a definition, as written, and raises UserError once the copies pass 250,000 values, before building the rest. Walks without prefer_inlined_defs are not limited.
    Repro: test_inlining_past_the_value_limit_is_refused_before_the_request, a tool schema of a few dozen values that main inlines without a limit.
  • Decision-model fields. _fields now counts each definition a field resolves to with the same InlinedValues counter type and limit, and stops with UserError, which the decision model already treats as a route it cannot fill.
    Repro: test_an_output_type_past_the_value_limit_is_refused, which main resolves in full and sends.
  • MessagesBuilder.add rebuilt a message's parts list on every call, which is quadratic in the parts of one message. It now appends in place to the list it made, and copies a list it did not make once.
    Repro: test_messages_builder_extends_its_own_parts_in_place; main makes a new list on every add.
  • private_key in DEFAULT_SECRET_PATTERNS. Header lines are now read once each: a line runs to its real line break, or, in a key pasted with escaped newlines, to the last escaped one, and in a key with real line breaks the whole header section up to its blank line is taken together.
    Repro: test_a_run_of_backslashes_in_a_header_is_scanned_in_linear_time and test_a_run_of_header_lines_is_scanned_in_linear_time, each seconds on main.

The larger part is core, so this carries pkg:core; the redact_secrets change is in pydantic-ai-harness. #8620 also touches _json_schema.py.

[!WARNING]
Compatibility impact: the private_key entry in DEFAULT_SECRET_PATTERNS has a new value. Code that compares or copies that pattern sees the new one.

Why this can ship in a minor release: the pattern's text is not a documented contract, and the new one redacts the same key material or more on every key shape tested.

Migration: none.

New public surface

None.

Verification

  • Limit: 250,000 JSON values per schema. The largest real schema measured, the Anthropic SDK's beta MessageCreateParams, copies 26,868; its non-beta MessageCreateParams 9,715, OpenAI's Responses create params 7,936, list[ModelMessage] 3,756 and AG-UI's RunAgentInput 1,563. A schema past the limit raises UserError while the request is prepared.
  • These fail on main and pass here: test_inlining_past_the_value_limit_is_refused_before_the_request and test_inlining_counts_every_value_of_every_copy in tests/test_json_schema.py; test_arguments_past_the_value_limit_are_not_filled and test_an_output_type_past_the_value_limit_is_refused in tests/models/test_decision.py; test_messages_builder_extends_its_own_parts_in_place in tests/test_ui.py; test_a_run_of_header_lines_is_scanned_in_linear_time, test_a_run_of_backslashes_in_a_header_is_scanned_in_linear_time, and the real-newline cases of test_a_key_with_a_header_value_broken_across_lines_is_taken_to_its_end_marker in tests/harness/guardrails/test_detectors.py.
  • These pass on both and guard behavior that must not change: test_a_schema_walked_without_inlining_is_not_limited, test_messages_builder_copies_parts_it_did_not_make, test_an_escaped_encrypted_key_with_no_end_marker_is_still_taken, test_end_marker_text_in_a_header_value_does_not_end_the_key, test_a_backslash_n_in_a_header_of_a_key_with_real_line_breaks_is_text, test_a_key_mixing_real_and_escaped_line_breaks_is_taken and test_end_marker_text_after_a_key_with_no_end_marker_leaves_the_prose_alone.

What changes for existing users

  • A tool or output schema whose inlined $refs copy more than 250,000 JSON values now raises UserError while the request is prepared, on models that inline $defs.
  • On a decision model, such a route is one it cannot fill. A lone output type like that raises UserError before any request is sent.
  • MessagesBuilder.add now grows the parts list it made in place, so a reference to that list sees later parts.
  • redact_secrets / secret_data now redact more of a private_key block:
    • A key pasted with escaped newlines, with an escaped blank line before its body, is now redacted even without headers or an END marker.
    • A key with real line breaks whose header section holds a line that is not a header is now redacted whole, including any text in that section up to its blank line.
    • A block of header lines with no body after them is now redacted, in either line-break style.
PR decisions
  • 2026-11-03 · The limit counts JSON values, not schema nodes, so it bounds what copied definitions carry (enum, default, const) too; one counter type and limit serve inlining and decision-model field resolution. (Session: 3a14e78f-adea-4997-a53e-d6c45aab2d7d)
  • 2026-11-03 · The limit is 250,000 JSON values per schema, not a total across a request's tools. (Session: 3a14e78f-adea-4997-a53e-d6c45aab2d7d)
  • 2026-11-03 · Each definition is counted when it is copied, and a cached definition's later copies count what its walk counted, so the walk stops before building past the limit. (Session: bbcb3e70-1ab6-4191-88ed-0dc29c45820b)
  • 2026-11-03 · A lone decision-model output type past the limit raises UserError up front rather than after a request. (Session: 3a14e78f-adea-4997-a53e-d6c45aab2d7d)
  • 2026-11-03 · The private_key changes are accepted: across 97 key shapes and 200,000 random blocks it never redacts less key material than before. (Session: 3a14e78f-adea-4997-a53e-d6c45aab2d7d)

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. The release note is left to the release manager.
  • PR title is fit for the release changelog.