Repository navigation
The Anthropic client sends a JSON Schema as the Messages API's structured output - #1311
pauloeduardo wants to merge 1 commit into
Conversation
…ured output A json_schema response format was dropped, so the conflict resolver's Claude answers came back in a shape parse_verdicts could not read and every judgement failed while still being billed. The schema now goes in output_config.format and into the JSON directive, which keeps the shape when a model refuses output_config. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Paulo Budal <1210892+pauloeduardo@users.noreply.github.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
zlareb1
left a comment
There was a problem hiding this comment.
An effort refusal prevents subsequent requests from sending their schema. I reproduced this on the reviewed head with a stubbed Messages response and the conflict resolver’s schema.
| output_config["effort"] = settings.llm_anthropic_effort | ||
| if schema is not None: | ||
| output_config["format"] = {"type": "json_schema", "schema": schema} | ||
| if output_config and "output_config" not in t.refused: |
There was a problem hiding this comment.
P2 — A 400 naming output_config.effort caches a refusal of the entire output_config for the model. A subsequent conflict-resolver request drops format even if the provider accepts structured output. I reproduced this by sending a json_object request with LOOM_LLM_ANTHROPIC_EFFORT=low, returning an effort-only 400, then sending the resolver’s RESPONSE_FORMAT to the same client. Cache an effort-only refusal separately and test that the next request still sends format.
There was a problem hiding this comment.
The Python SDK snapshot and the code.read classification failures reproduce on clean main. Both assertions pass with the tracked repair. Snapshot failure · Isolation failure · Repair #1293.
|
|
Closes: #1275
Changelog category (leave one):
Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):
The Anthropic client sends a caller's JSON Schema as the Messages API's structured output, so the conflict resolver now judges memories when
LOOM_CONFLICT_RESOLVER_MODELnames a Claude model. Previously every judgement failed withnothing usable in two answersand was still billed.Why
The conflict resolver passes
response_format={"type": "json_schema", ...}and parses{"peers": [{"peer_id", ...}]}.AnthropicLLMClient.completehonoured onlyjson_objectand dropped every other format. The resolver's system prompt says "Return JSON matching the schema" without spelling the schema out, so Claude answered with a correct judgement in its own shape: a top-level array whose entries carryid.parse_verdictsread nothing from it, the retry at twice the cap returned the same shape, and the row stayed owed and was paid for again on every pass.The fix is in the client, not the resolver's prompt, so every caller that passes a schema gets it on Anthropic:
json_schemaformat becomesoutput_config.format = {"type": "json_schema", "schema": ...}, beside the effort the client already sends inoutput_config. The Messages API constrains the reply to the schema.output_config, the existing refusal learner drops the whole block, and the effort and format go with it. The directive keeps the reply in the caller's shape in that case.json_schemareply also gets the code-fence stripping thatjson_objectreplies get.Alternatives considered:
tool_choicereturns a 400 on current Claude models.Scope and risk
This touches
server/llm/anthropic.py, its unit tests and the structured-output paragraph ofdocs/architecture/llm-architecture.md. The OpenAI, local and Bedrock clients are unchanged. Ajson_objectrequest is sent as before: the directive alone, and nooutput_config.format.Risk: a schema keyword the Messages API does not accept returns a 400 that names
output_config. The refusal learner then dropsoutput_configfor that model, effort included, and the directive carries the shape. The one schema in the tree, the resolver's, uses only types,enum,requiredandadditionalProperties: false, all of which are accepted.Compatibility, rollout and rollback
output_config.formatwhen the caller passes ajson_schemaformat.LOOM_MEMORY_CONFLICT_RESOLVER_ENABLED=falsewith a Claude resolver model) can turn it back on.Tests
LOOM_ENV_FILE= .venv/bin/python -m pytest tests/unit/test_llm_anthropic.py tests/unit/test_conflict_resolver.py tests/unit/test_conflict_resolver_holds.py tests/unit/test_doc_links.py tests/unit/test_public_copy.py -q: 485 passed. Three tests are new:output_config.formatbeside the effort, and is written into the directive;output_configrefusal the re-sent request keeps the schema in the directive;json_objectsends nooutput_config.format.make lintandmake typecheckpass. The JSON-mode setup moved into_json_requestto keepcompleteunder the function-length gate.make formatneedsshfmt, which this machine lacks;ruff formatandprettier --checkon the changed files pass.Against the live Messages API, the reproduction from the issue now parses. It is the resolver's
SYSTEM_PROMPT,user_promptandRESPONSE_FORMATwith one note and two peers, thenparse_verdicts:claude-haiku-5-5:{'m1': ('update', 0.93), 'm2': ('related', 0.9)}claude-sonnet-5-5:{'m1': ('update', 0.93), 'm2': ('related', 0.85)}Before the change, both returned
{}.AI assistance in the code (leave one; writing this description with AI is fine):
AI-generated (an agent wrote most of the diff): Claude Code wrote the change and the tests. The live-API reproduction above was run against the branch.
I can explain every line of this diff and how it interacts with the rest of Loom.
Checklist
make test-unitand applicable integration results are reported by CI; a full local run is not required before pushing.main; no migration in this change.docs/describe the system as it now is, and this description names any operator steps and wire changes.tests/unit/test_public_copy.py).git commit -s), as CONTRIBUTING.md asks.