Expand description
OpenRouter normalization Batch API and synchronous embedding client.
BatchTransport owns normalization submission and polling.
EmbeddingTransport owns synchronous array embedding requests.
API rules encoded here, verified against the live API on 2026-08-29
(.agents/skills/openrouter-api/ and the batch quickstart):
- One batch carries one
endpoint, one batch-levelmodel, and one inlinerequestsarray. The body must serializeendpointandmodelbeforerequests: the API stream-parses and answers 400 otherwise.SubmitBatchfixes that order by field declaration; a test pins it. - Success is any 2xx with a parseable batch body: submit answers
202 Acceptedwithstatus: "validating"— queued, not complete. - Terminal statuses are
completed,failed,expired,cancelled. Results arrive inline in the poll response; there is no download endpoint. Acompletedbatch can still contain per-request failures: exactly one ofresponseorerroris populated per result line. - Result lines decode per line: a line that breaks the documented
envelope arrives as
BatchResultLine::Undecodablewith the raw JSON preserved, so one bad line never fails a completed batch. The result-side helpers areBatchResultLine::chat_contentandembedding_vectors. - The batch-level
modelmust be a:batchvariant slug; a base slug is rejected synchronously and fails the whole submission. - Server-side retention is 30 days: callers persist every result into Postgres at poll time.
The API root is configuration, not a constant (context/05): the default is the public endpoint; e2e points it at the local fake batch server.
Structs§
- Batch
Item - One request inside a batch: a deterministic join key plus the endpoint
body.
custom_idmust be unique within the batch. - Batch
Object - The batch object returned by both submit (202,
validating) and poll. - Batch
Usage - Batch usage.
costis dollars as a plain f64: the API emits scientific-notation floats (1.33e-05), so fixed-point types are wrong. - Embedding
Request - One synchronous embedding request with array input.
- Http
Transport - Message
- One chat-completions message.
- RawChat
Response - One synchronous chat-completions response: the provider payload exactly
as received, plus the request ID header when present. The eval harness
decodes it with
chat_completion_content— the same traversal the batch result lines use. - RawEmbedding
Response - A successful HTTP response before typed result validation.
- Request
Counts request_counts.completed + failed == totalis a completeness sanity check, not a success signal: failed requests stay in-band as result lines.- Result
Line - The documented result envelope: exactly one of
responseorerroris populated. - Result
Response - The per-request success envelope: an HTTP status plus the endpoint’s
response body (
chat.completionor embedding list). - Structured
Params - Parameters shared by every request in one structured batch. One schema per batch is mandatory on Google-backed routes (quickstart), so the schema lives here, at batch level, not per item.
- Submit
Batch - A batch submission.
Enums§
- Batch
Endpoint - The API shape every request in one normalization batch uses.
- Batch
Line Error - A per-line failure of one completed request. The
Displaystrings are the durablelast_errortexts operators see in Postgres. - Batch
Result Line - One completed request’s result line, decoded per line: a line that does
not match the documented envelope arrives as
Undecodablewith the raw JSON preserved, so one malformed line never fails a completed batch and callers can persist it and mark the matched request retryable (context/05). Unknown fields on a parsed line survive a decode/encode round trip (pinned by the decode tests). - Batch
Status - Batch status lifecycle:
validating -> in_progress -> finalizing -> completed, withfailed,expired, andcancelledas the other terminal outcomes. - Embedding
Decode Error - A decode failure of one stored embedding payload. The
Displaystrings are the durablelast_errortexts operators see in Postgres. - Transport
Error - Typed transport failure. Retry classification lives here so every caller shares one policy (context/11): timeouts, connect failures, 408/429, and 5xx are retryable; other 4xx and decode errors are not.
Traits§
- Batch
Transport - The transport seam: submit and poll, nothing else. Implementations must treat any 2xx with a parseable batch body as success (submit answers 202).
- Embedding
Transport - The synchronous embedding I/O seam.
Functions§
- chat_
completion_ content - The assistant message content of one chat-completions response body. Shared by the batch result decode and the synchronous client, so the two paths cannot drift on where the content lives.
- embedding_
vectors - Decode one stored raw embedding payload and return the vectors ordered
to match the request inputs:
vectors[i]belongs to inputi. Validates the envelope, the count, the index range, duplicates, and completeness. - structured_
body - One structured chat-completions request body: the exact body the Batch API wraps per item and the synchronous chat-completions endpoint accepts. Shared by both paths so their wire shapes cannot drift.
- submit_
structured - Submit one structured chat-completions batch: every item carries the same
response_formatjson_schema withstrict: true.