Skip to content

The Anthropic client sends a JSON Schema as the Messages API's structured output - #1311

Open
pauloeduardo wants to merge 1 commit into
ClickHouse:mainfrom
pauloeduardo:fix/anthropic-json-schema
Open

pauloeduardo wants to merge 1 commit into
ClickHouse:mainfrom
pauloeduardo:fix/anthropic-json-schema

Conversation

@pauloeduardo

Copy link
Copy Markdown

Closes: #1275

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in a release)

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_MODEL names a Claude model. Previously every judgement failed with nothing usable in two answers and was still billed.

Why

The conflict resolver passes response_format={"type": "json_schema", ...} and parses {"peers": [{"peer_id", ...}]}. AnthropicLLMClient.complete honoured only json_object and 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 carry id. parse_verdicts read 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:

  • A json_schema format becomes output_config.format = {"type": "json_schema", "schema": ...}, beside the effort the client already sends in output_config. The Messages API constrains the reply to the schema.
  • The same schema is written into the JSON directive appended to the system prompt. When a model or gateway refuses 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.
  • A json_schema reply also gets the code-fence stripping that json_object replies get.

Alternatives considered:

  • Spell out the shape in the resolver's prompt only. This fixes one caller and leaves the client dropping the format for any later caller.
  • Send the schema as a forced tool. Forced tool_choice returns a 400 on current Claude models.

Scope and risk

This touches server/llm/anthropic.py, its unit tests and the structured-output paragraph of docs/architecture/llm-architecture.md. The OpenAI, local and Bedrock clients are unchanged. A json_object request is sent as before: the directive alone, and no output_config.format.

Risk: a schema keyword the Messages API does not accept returns a 400 that names output_config. The refusal learner then drops output_config for that model, effort included, and the directive carries the shape. The one schema in the tree, the resolver's, uses only types, enum, required and additionalProperties: false, all of which are accepted.

Compatibility, rollout and rollback

  • Wire: no op or field changes. The Anthropic request gains output_config.format when the caller passes a json_schema format.
  • Operator steps: none. An install that disabled the resolver because of this (LOOM_MEMORY_CONFLICT_RESOLVER_ENABLED=false with a Claude resolver model) can turn it back on.
  • Rollback: revert the commit. Nothing is stored.

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:

    • a schema is sent as output_config.format beside the effort, and is written into the directive;
    • after an output_config refusal the re-sent request keeps the schema in the directive;
    • json_object sends no output_config.format.
  • make lint and make typecheck pass. The JSON-mode setup moved into _json_request to keep complete under the function-length gate. make format needs shfmt, which this machine lacks; ruff format and prettier --check on the changed files pass.

  • Against the live Messages API, the reproduction from the issue now parses. It is the resolver's SYSTEM_PROMPT, user_prompt and RESPONSE_FORMAT with one note and two peers, then parse_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

  • Local format, lint, type checks and focused tests pass. The full make test-unit and applicable integration results are reported by CI; a full local run is not required before pushing.
  • Generated files are regenerated in this PR, not by hand: none are affected.
  • A new migration is additive and numbered after the latest on main; no migration in this change.
  • No plugin file changes.
  • No frontend changes.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

…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>
@vercel

vercel Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
loom-docs Ready Ready Preview Nov 24, 2026 12:20pm UTC

Request Review

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread server/llm/anthropic.py
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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@zlareb1 zlareb1 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@cla-assistant

cla-assistant Bot commented Nov 24, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

conflict_resolver never judges with an Anthropic model: the Anthropic client drops the json_schema response_format

2 participants