Testing LLM integrations: your mock is a contract you wrote yourself

Two paths from the same code: through a fake model to green tests, and through the real provider’s strict schema check to a staging run with no answer.

In October I built an AI layer for a fintech platform solo, in two weeks. By the end it had about 6,700 tests at 96–97% coverage. For part of that time, not one question on staging got an answer.

Nothing was wrong with the tests. They did exactly what tests against a fake model do: they checked my code against my own idea of how the provider behaves. A fake LLM is a contract you wrote yourself, and coverage says nothing about whether that contract is true. Three clauses of mine weren’t. Each is cheap to test once you treat it as a contract rather than an implementation detail.

The boundary you don’t own

Most of an LLM feature is ordinary code: routing, retries, state, parsing, persistence. Fakes are the right tool for that, and I wouldn’t give them up – they’re fast, free and deterministic, and they let you run the orchestration thousands of times a day.

What a fake can’t tell you is anything the provider decides. Four things cross that line on every call: the schema dialect, which parameters a given model accepts, how a non-answer is signalled and how a fallback model differs from the primary. The provider owns all four, documents them unevenly and changes them without asking you. That is where all three bugs lived.

Assumption 1: strict mode speaks JSON Schema

It doesn’t. It speaks a dialect of it.

Our answers came back as structured output. One field was a map whose keys were constrained with propertyNames – valid JSON Schema, and rejected in strict mode by both OpenAI and Anthropic. The fake accepted it, the unit tests passed, and on staging every call that used the schema failed at the provider, taking every question down with it.

The documentation explains it once you look. OpenAI publishes a list of supported keywords and requires additionalProperties: false on every object; Anthropic’s limitations allow additionalProperties only as false. An open-ended map has no valid encoding in either.

We fixed it at two levels, and the split matters:

  • A static check on every commit walks every schema sent to a model and fails on anything outside the dialect. It is fast and deterministic, and it knows only what the docs and past incidents have taught it.
  • A live call to the real models after any schema change runs from the worker’s own container, so it uses the production SDK versions and settings. It catches what the static rules don’t know yet.

I keep the live check off the per-commit path on purpose. It is slow, it costs money and its output is non-deterministic, so it asserts only that the provider accepts the request and returns a valid object – never what the object says. Triggering it on a schema diff keeps it cheap enough that nobody is tempted to skip it.

Assumption 2: the schema is data

It is prompt. The model reads field names and descriptions as instructions, and so does the provider’s safety layer.

The model that wrote client-facing text began returning content_filter, consistently, on a schema for a draft of a text. The prompt was unremarkable. The cause was the description on two objects in the schema; removing them stopped the refusals. Nothing on our side of the fake could have shown that.

The more expensive part was what our code did with it. A refusal isn’t an HTTP error: Anthropic returns a 200 with stop_reason: "refusal", and the body may not match your schema; OpenAI adds a separate refusal field. We parsed it as an answer, it failed validation further down and surfaced as a schema error nowhere near its cause.

The fix was to classify the outcome once, at the adapter, before anything tries to parse it:

class Outcome(Enum):
    OK = "ok"
    REFUSED = "refused"
    EMPTY = "empty"

def classify(choice) -> Outcome:
    # LiteLLM normalises every provider to the OpenAI response shape.
    if choice.finish_reason == "content_filter" or getattr(choice.message, "refusal", None):
        return Outcome.REFUSED
    if not choice.message.content:
        return Outcome.EMPTY
    return Outcome.OK

A refusal or an empty answer counts as a provider failure and moves the call to the fallback model; only OK reaches the parser. Putting the decision in one place also settled who retries. Retries inside LiteLLM are switched off and the harness owns them, so every attempt – including the refused one – is its own recorded step rather than something that happened invisibly inside a library.

Assumption 3: the fallback is the same integration

It is a second integration with its own limits.

The primary model for short card texts started looping when temperature was below 1.0. Dropping the parameter was the obvious fix, and then we checked the fallback: it doesn’t accept temperature at all. Every time the primary failed, the fallback would have failed too, for an unrelated reason. Fallback paths are the least-exercised code you have, and this is how they rot.

What made it testable was a decision taken for other reasons: model choice is a fixed policy from task class to models, not a semantic router. That turns ‘which model might serve this step’ into a finite list, so the parameters can be checked against every entry:

@pytest.mark.parametrize("step", MODEL_STEPS, ids=lambda s: s.name)
def test_step_params_are_valid_for_every_model_in_its_class(step):
    for model in MODEL_POLICY[step.task_class]:  # primary first, then fallbacks
        unsupported = set(step.params) - SUPPORTED_PARAMS[model]
        assert not unsupported, f"{step.name} → {model}: {sorted(unsupported)}"

The last tier isn’t a model at all. If both fail on a card, the card falls back to a fixed text written in advance and the run continues. Deciding that up front – which outputs may degrade and which must fail the run – is a product decision, and it belongs in the design, not in an exception handler.

What I would do from day one

Two things I didn’t do on this project and would do next time.

Make the fake strict. The schema check and the parameter matrix shouldn’t live only in their own tests; the fake should run both on every request. Then every unit test enforces the provider contract for free, and a new schema can’t reach staging without passing the dialect rules at least once.

Keep the rules as a changelog, not a list. A denylist will always lag behind the providers. Each entry should point to the doc section or the incident that added it, so when a provider relaxes a rule you can tell which checks still earn their keep.

The point

None of the three bugs was clever. All three passed a green suite, because the suite measured my code and nothing measured my assumptions about someone else’s. When a test double stands in for a system you don’t control, write down what it assumes and test those assumptions against the real thing – sparingly, on the changes that can break them.