Repository navigation
Description
Context & Rationale
Three in-house "parse, validate, repair, reflect, retry" implementations exist beside pydantic_ai's native one. Checked at 9c879ae.
ai/client/structured.py(chat_structured:215, repair:105, reflection:123) has no source caller.ai/agents/runner.py:431-496(_handle_schema_validation_retry,_validate_agent_output) withai/agents/agent.py:887-990and:1086-1095is the path the review takes.ai/agents/pipeline.py:63-76(_parse_pipeline_output) then re-parses the same reply withextract_json_blockplusmodel_validateand downgrades failures to a warning.- One validated structured reply in a review crosses 14 files:
commands/review.py→ai/review/runner.py→ai/review/pipeline.py:2201→ai/agents/pipeline.py→ai/agents/agent.py:778→ai/client/unified.py:681→ai/client/ollama.py:209→ai/client/network.py→ai/client/base.py→ai/client/models.py→ai/response_repair.py→ai/agents/runner.py→ai/schema_reflection.py→ai/review_schema.py. The module named "structured" is not on it. - feat(ai): one json repair seam that counts repairs per entry point #875 counts repairs across the json-repair call sites. This item is the module those calls belong to.
Deletion test: structured.py deletes clean. Deleting the agent.py/runner.py path makes it reappear in the loop, so that is where the one implementation concentrates.
Scope & Deliverables
- Deliverable: one module validates model text against a schema under a bounded repair-and-reflection retry policy and returns either the model or the next retry message. The agent loop calls it;
MultiAgentPipelinestops re-parsing;chat_structuredbecomes a thin verb over it or goes. - Tests:
test_ai_client_structured.py(12),test_schema_reflection.pyand the output-retry tests intest_pydantic_agent.pycollapse into one table-driven suite. - Key questions (open):
- The three retry semantics differ subtly; which policy survives, and is pydantic_ai's native output validation the implementation?
- Does feat(ai): one json repair seam that counts repairs per entry point #875's repair counting become a property of this module?
- Constraint: feat(ai): sequential pipeline stages carry a budgeted context, with the scratchpad appended once #859 changes what
MultiAgentPipelinecarries between stages; sequence the two.
Strategic Alignment
- Design: none
- Depends on: none
- Related: feat(ai): one json repair seam that counts repairs per entry point #875, feat(cli): lossless structured error reflection for schema retries #437, feat(ai): sequential pipeline stages carry a budgeted context, with the scratchpad appended once #859
- Source: architecture review of 2026-10-02, AI core
Scope added 2026-10-02: checked at 38517a8; the claims hold. chat_structured (ai/client/structured.py:215) is reachable as an LLMClient method through StructuredOutputMixin (ai/client/unified.py:161), but nothing calls it. _parse_pipeline_output (ai/agents/pipeline.py:63-73) logs a failed validation as a warning and returns None. JSON repair runs from ai/client/structured.py, ai/agents/agent.py, ai/agents/models.py, ai/review_schema.py and core/serialization.py. core.serialization.extract_json_block falls back to json_repair (core/serialization.py:76-80), and all 7 of its callers pass model replies (ai/review/verification.py:2057, ai/agents/pipeline.py:68, ai/review_schema.py:1376, ai/analyze/outlines.py:124, ai/benchmark/runner.py:593, ai/review/runner.py:527, :549). #875 (P2, backlog, refined) already owns one repair_json(text, *, source) entry in ai/response_repair.py, json_repair imported only there, the devops_cli_ai_json_repairs_total counter per entry point, and deleting chat_structured with its helpers and tests. This item does not repeat that work. The review evaluation of 2026-10-02 (section 4.3, N1) found that persona replies are cached with no validator (ai/agents/agent.py:887), so a reply that echoes the schema is replayed for 7 days. #1013 (filed 2026-10-02, P1) is that fix: the response cache stores and serves only replies the review can use. Its validator rejects a reply before it is cached; this item later owns the validation that validator runs. #859 (v0.2.27) changes what MultiAgentPipeline carries between stages.
Acceptance criteria
- One module validates model text against a schema: parse, repair, validate, reflect the error, and retry under one bounded policy. It returns the model or the next retry message. The agent loop calls it, and no other code in
srcvalidates a model reply against a schema. -
StructuredOutputMixinis gone fromLLMClient's bases. feat(ai): one json repair seam that counts repairs per entry point #875 deleteschat_structuredand its helpers first. -
MultiAgentPipelinedoes not parse a stage's reply again._parse_pipeline_outputis deleted. A stage whose reply fails validation is reported as unparsed, with the validation error, in the stage result. A test fails at 38517a8, where the failure is only logged. - Schema-validated replies are repaired through feat(ai): one json repair seam that counts repairs per entry point #875's one repair entry, called from this module.
fix_llm_response(called today fromai/agents/agent.py:899,:1089andai/review_schema.py:1373) is called only from this module. An AST test checks it. - The module counts validation retries per entry point beside feat(ai): one json repair seam that counts repairs per entry point #875's repair counter, and the review's
profile.jsonrecords both. - A reply the validator rejects is never written to the response cache. fix(review): a group-analysis reply that fails its checks is neither cached nor served #1013's tests pass unchanged.
-
tests/test_schema_reflection.py, the output-retry tests intests/test_pydantic_agent.pyand whatever oftests/test_ai_client_structured.pyfeat(ai): one json repair seam that counts repairs per entry point #875 leaves become one table-driven suite. Each row is a reply text and the expected result or retry message. - The tests run offline.
-
uv run devops cipasses.
Key questions
- The three retry semantics differ subtly; which policy survives, and is pydantic_ai's native output validation the implementation? (proposed) The agent loop's policy (
ai/agents/runner.py:431-496, with feat(cli): lossless structured error reflection for schema retries #437's lossless error reflection) survives, because it is the only one production runs. pydantic_ai's native validation is not the implementation, because no production code runs a native pydantic_ai agent at 38517a8 (see refactor(ai): one agent runtime behind one construction point, with no silent fallback #1005). - Does feat(ai): one json repair seam that counts repairs per entry point #875's repair counting become a property of this module? (proposed) No. feat(ai): one json repair seam that counts repairs per entry point #875 is refined and smaller: it puts every
json_repaircall behind one entry inai/response_repair.py, counts repairs there by entry point, and deleteschat_structured. It lands first. This module then calls that entry and adds the retry count, so nothing is counted twice. - Does fix(review): a group-analysis reply that fails its checks is neither cached nor served #1013 (the cache stores only replies the review can use) land first? (proposed) Yes. It is a measured defect, and this item keeps its tests.
- When does it land? (proposed) After feat(ai): one json repair seam that counts repairs per entry point #875, feat(ai): sequential pipeline stages carry a budgeted context, with the scratchpad appended once #859 (v0.2.27) and fix(review): a group-analysis reply that fails its checks is neither cached nor served #1013.
Update 2026-10-08: refinement
chat_structuredruns in production (intake, refine), and fix(roadmap): refine sends the model the proposal schema and never caches a reply that fails it, and one failed item skips only that item #1363 added the prompt schema,constrain_replyand the first-attempt validator (ai/client/structured.py:159,:241,:310); the one path keeps all three.- 'StructuredOutputMixin is gone' becomes: it leaves
LLMClient's bases once intake and refine use the new module. 'fix(review): a group-analysis reply that fails its checks is neither cached nor served #1013's tests' become fix(roadmap): refine sends the model the proposal schema and never caches a reply that fails it, and one failed item skips only that item #1363's cache tests (fix(review): a group-analysis reply that fails its checks is neither cached nor served #1013 is frozen). - Owner decision on refactor(ai): one model seam: llmclient over three wire adapters behind one factory, no second provider stack #1001: structured output uses pydantic-ai's output modes (feat(ai): constrain structured replies on the claude, openai and copilot providers #1419). (proposed) This module keeps repair and retry only while
PydanticAgentruns.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsReady
- StatusShow more project fieldsReady
Activity
dan-petty commentedon Oct 8, 2026
This item's premise is out of date (checked 2026-10-08 at 910b823).
chat_structured"has no source caller". Since v0.2.26, roadmap intake (src/devops_cli/roadmap/intake_model.py:184) and refine (src/devops_cli/roadmap/refine.py:344,:792) call it in the roadmap service.chat_structured's reflect-and-retry also runs in production.Two more things changed since refinement:
chat_structured. It appends the schema to the system message, sends aresponse_formatof typejson_schemaon the gateway provider, and sendsformaton the ollama provider. It also keeps invalid first replies out of the response cache.Suggested amendment:
StructuredOutputMixinis gone" holds only once intake and refine call the new module.dan-petty commentedon Oct 28, 2026
Re-scope, checked on 2026-10-28 at origin/main 4226904. The goal of this item stays: one path validates, repairs, reflects and retries a structured reply. But the premise of its design does not hold now, so the item is built by deletion, not by a new module.
Evidence
chat_structured(ai/client/structured.py) is the only structured-output path that production code calls: roadmap intake (roadmap/intake_model.py:235), refine (roadmap/refine.py:477,:1115) and research (ai/research/mental_model.py:228,ai/research/socratic.py:147).srcgives aPydanticAgentanoutput_type, anoutput_schemaor anoutput_validator(rg -n "output_type=|output_schema=|output_validator\(" src). The two agents that production builds (commands/ai.py:1103,:1292) have neither. The review builds no agent since the evidence-first redesign (feat(review): evidence-first code review — tools find, version-pinned facts ground, models explain and plan, fixes are re-checked #1137). So_handle_schema_validation_retry,_validate_agent_outputandis_structured_output_schema(ai/agents/runner.py), the schema arm offix_llm_response(ai/response_repair.py) andai/output/run only in tests.MultiAgentPipeline(commands/ai.py:1284) sets nooutput_schema, so_parse_pipeline_outputreturns None on every production run, anddevops aiprints nofinal_data._build_reflection_message(ai/client/structured.py) andbuild_schema_reflection_message(ai/schema_reflection.py) build the same message.pydantic_ai.Agent, whoseoutput_typevalidates and retries natively. A new hand-written module that both loops call would be a third layer to delete then.Re-scope (built in one pull request)
output_type,output_schema,output_json_schemaandoutput_validatorofPydanticAgent, the three runner helpers, the schema arm offix_llm_responseandai/output/.MultiAgentPipeline.output_schema,_parse_pipeline_output,final_dataandPipelineStepResult.parsed_data.chat_structuredusesbuild_schema_reflection_message, and its own copy goes.chat_structuredstays the one path. Its tests (intake, refine, the fix(roadmap): refine sends the model the proposal schema and never caches a reply that fails it, and one failed item skips only that item #1363 cache tests) pass unchanged.The open questions, decided
chat_structured's, because it is the only one production runs. pydantic-ai's native output validation becomes the implementation when refactor(ai): one agent runtime behind one construction point, with no silent fallback #1005 adopts the native agent. This item builds no bridge to it.ai/response_repair.py, andchat_structuredalready counts each attempt by outcome (devops_cli_ai_structured_attempts_total). No new counter.profile.jsonrecords the retries: dropped. The review makes no structured-output call.chat_structured's request for three providers. This item deletes unused paths. Neither needs the other first.