AI plumbing fix: send nullable enums to Anthropic as anyOf

Merged
LU
9d ago
+31-2

A fix for the AI plumbing series, restacked on #5504 on top of #5608 (feat/ai-5077-followups). It touches only the Anthropic transport and its test.

Problem

On native Anthropic (LLM_PROVIDER=anthropic with an Anthropic key), categorization, bill setup and PDF import fail on every call. The API answers 400 before the model sees the request:

output_config.format.schema: Invalid schema: Enum value 'Salary' does not match declared type '['string', 'null']'

Anthropic's native structured output refuses an enum on a field whose type is a list with "null". That is how CategorizeTransactions, SuggestBillSetup and the PDF extraction schema declare a nullable enum, and how OpenAI's strict mode wants it written. RubyLLM's with_schema passes the schema through unchanged.

What a user sees: the job is discarded with Ai::Error::Refused, a failed row appears on the LLM usage page, and the transactions stay uncategorized. PDF import shows "No model set up on this instance can read this PDF". Merchant detection, merchant enhancement and chat are not affected.

OpenAI and OpenAI-compatible gateways accept the schema, so the recorded Kilo runs (including ~anthropic/claude-haiku-latest, which goes through the compatible transport) pass. The native Anthropic routes in test/models/ai/live/generate_live_test.rb catch it, but they skip without a key.

On main the same key works: the old Provider::Anthropic workers force a tool call, which does not validate the schema this way.

What changes

Ai::Transport::Anthropic#native_schema already rewrites what Anthropic's structured output refuses (numeric limits go into the description). It now also sends a field typed [x, "null"] with an enum as:

{ "anyOf": [ { "type": "x", "enum": ["the", "non-null", "values"] }, { "type": "null" } ] }

The rewrite is in the transport, not in each feature's schema: it is one provider's quirk, and any later nullable enum gets it too. The features' own schemas are unchanged, so Ai::Schema still checks the answer against them.

One unit test covers the request shape.

Verification

On feat/ai-5077-followups @ 680608798 plus this commit, Ruby 3.4.13, 2026-11-24:

  • full bin/rails test: 19,185 runs, 209,971 assertions, 0 failures, 0 errors (33 skips);
  • bin/rails zeitwerk:check, bin/rubocop on the two changed files and Brakeman: clean;
  • live, with a native Anthropic key (AI_LIVE=1 bin/rails test test/models/ai/live/generate_live_test.rb): categorize and bill setup pass on claude-haiku-4-5, claude-sonnet-5-5 and claude-opus-5-5.

Earlier, on refactor/ai-plumbing @ fbe98e56b (2026-11-22):

  • without the fix, claude-haiku-4-5 fails categorize and bill setup with the error above;
  • with the fix, live through the real entry points on claude-sonnet-4-6 (the default): categorization and merchant detection through their jobs, bill setup through AiSetupSuggester, and PDF import of bac_ocr.pdf read as text, pdf and images (13 rows each, checks pass).

Not run locally: system tests, erb_lint and Biome (no view or JavaScript change).

Not included: a recorded native Anthropic cassette, so CI still does not replay this path. I have one for bill setup on a re-sliced copy of the stack (https://github.com/luis-ibanez/sure/pull/1, a separate proposal about how the series lands) and can add it here if useful.

šŸ¤– Generated with Claude Code