Files
Salah Eddine Bekhouche 41736cbab0 feat(router): reject previous_response_id on a router collection (#3497)
* feat(router): reject previous_response_id on a router collection

A response chain is state held inside the backend that created it, so
`previous_response_id` cannot survive a turn routing to a different candidate
than served the last one. `/v1/responses` now returns 400 with
`router_response_chain_unsupported` when the addressed model is a
collection.router, before dispatch and before any model load, rather than
letting a new candidate answer without the chain's context.

The check reuses a single `router_collection_info()` helper that dispatch also
calls, so the guard and dispatch cannot disagree about what counts as a router
collection. Scoped narrowly: an empty or non-string `previous_response_id`, a
concrete model, and every other endpoint are all unaffected.

Guard 2 of #2957. Guards 1 and 3 are deliberately left out — the tool-loop
condition needs a schema key, and responses-capability filtering belongs on
top of the candidate-filter framework #3339 is introducing.

* fix(router): resolve the collection once for the responses chain guard

The guard and router_dispatch() each looked up the collection, so a
collection registered again between the two reads could pass the guard,
and every Responses request copied its ModelInfo twice. validate() now
resolves it once and passes it to a router_dispatch() overload.

The guard also requires a parsed routing policy, as dispatch does, so a
collection whose config failed to parse reports that problem instead of
the chain error.

* fix(router): refuse every reference to earlier responses on a router collection

A Responses request can also point at state held by the backend that
served an earlier turn through conversation, or through an input item of
type item_reference. Both cross candidates the same way
previous_response_id does, so the guard now checks all three.

The error now tells the client to send the request to the model the
earlier response reports. The rejection is logged at INFO, since it is
an expected client error. The explanation moves from the argument row
into the route notes and the error row, so the row matches its
neighbours.

* test(router): make the responses chain tests check what they claim

Test 641 only checked that the router error was missing from the body,
so a 404, a load failure or a timeout also passed, and it ran real
inference on Tiny-Test-Model. It now names a model that does not exist
and expects model_not_found, which shows the guard let the request
through without loading anything. Test 642 checks the error code
instead of searching the body text.

New tests cover a streaming request, which must get the JSON 400
before any SSE is sent, the collection named without its user. prefix,
and the conversation and item_reference references.

The previous_response_id row now says llama.cpp rejects the field
instead of implying concrete models support chaining.

* fix(router): catch item references without a type in the chain guard

An input item whose type was not a string made the guard throw, and the
client got a plain-text 500 instead of a JSON error. OpenAI also lets
an item reference leave out its type, so {"id": "msg_..."} went past
the guard. Both are now handled; a message item that carries its own
id still has a role or content and is not mistaken for a reference.

The error and the route notes now point at the earlier response's
x_lemonade_route.route_to, since its model field can be a checkpoint
path rather than a registered name.

* fix(router): tell streaming clients where the chain guard's candidate is

A streamed response carries x_lemonade_route on its first event, not on
the completed response, so the error and the route note now say so. A
new test checks that a full message carrying its own id is resent input
and passes the guard.

* fix(responses): log a failed router lookup and tighten the chain checks

When router_collection_info() threw in validate(), the error was
dropped and dispatch was skipped without a trace; log the same warning
the other routes log. Lead the rejection with resending the full
input, since llama.cpp rejects previous_response_id and the named
candidate is only worth trying when it supports continuation. Count a
conversation object as a reference only when its id is non-empty, and
list the standard headers first.

* test(router): stop waiting on generation in the chain-guard tests

Tests 642 and 646 only need to know the guard let the request
through, so stream it and close once the status is known instead of
waiting up to 600 s for a full reply. Fail with the status and body
when an error is not JSON, and cover a conversation object with an
empty or null id.
2026-11-20 07:09:12 +00:00
..

Lemonade Endpoints Spec

The Lemonade HTTP service provides a wide array of standards-compliant and custom endpoints.

Our design philosophy is:

  1. Ensure that Lemonade works out-of-box with all popular local AI apps.
  2. Prioritize using a pre-existing standard for functionality whenever possible.
  3. Add sufficient custom functionality to enable developers to build highly polished experiences.

This spec details all supported endpoints. It is organized into pages that correspond to which organization (OpenAI, Ollama, Lemonade, etc.) defined the endpoints.

API Description
OpenAI-Compatible API Start here for the main API surface used by most SDKs and clients.
Ollama-Compatible API Use this if your client expects Ollama-style behavior and routes.
Anthropic-Compatible API Use this for clients built around Anthropic's message format.
MCP Gateway Use this to expose Lemonade as a Model Context Protocol server (POST /mcp).
llama.cpp-Specific API Reference for llama.cpp-specific compatibility and conventions.
Lemonade-Specific API Local-first API for managing lifecycle, configuration, backends, etc.
Router API How collection.router models route requests, and how to test a routing policy.
Internal API Control endpoints for clients bundled with the server, secured by LEMONADE_ADMIN_API_KEY.

A running server also serves these pages itself: GET /v1/docs lists what it has and GET /v1/docs/{page} returns one, so the reference always matches the installed version and stays available offline.

Authentication

When LEMONADE_API_KEY is set, clients send it in the Authorization header as a Bearer token:

curl http://localhost:13305/v1/models -H "Authorization: Bearer $LEMONADE_API_KEY"

Each route says whether it needs a key. The internal routes take LEMONADE_ADMIN_API_KEY when it is set, and LEMONADE_API_KEY otherwise; the admin key also works on every other route. Lemonade reads no other header, so an Anthropic SDK client passes the key as auth_token, not api_key. The WebSocket APIs take the key as an api_key query parameter instead. See API Key and Security for every combination of the two keys.

Errors

Each route lists the errors it answers with in its Errors table. These come from the server before a request reaches a route, so no route lists them:

Status Body When
401 {"error": "Invalid or missing API key"}, or "Invalid or missing admin API key" for an internal route The request lacks the key its route needs
403 {"error": "Origin not allowed"} The Origin header names an origin that allowed_origins does not allow
404 {"error": {"message": "The requested endpoint does not exist", "type": "not_found", "path"}} No route matches the path
405 {"error": "Method Not Allowed. Use POST for this endpoint"} A GET reaches a POST-only route under /v0/, /v1/, /api/v0/ or /api/v1/