Skip to navigation

Create a chat completion (OpenAI Chat Completions format)

Use this when your client speaks OpenAI Chat Completions. Point the OpenAI SDK at MEADOW_BASE_URL/v1 and use a Meadow key as the API key. The OpenAI Python SDK was checked this way against the hosted service.

import os
from openai import OpenAI
client = OpenAI(base_url=os.environ["MEADOW_BASE_URL"] + "/v1", api_key=os.environ["MEADOW_API_KEY"])
reply = client.chat.completions.create(
model="gemini-3.1-flash-lite-preview",
max_tokens=16,
messages=[{"role": "user", "content": "Reply with the single word: meadow"}],
)
print(reply.choices[0].message.content)

The body is the OpenAI Chat Completions request. Meadow reads model, models, provider and stream, removes models and provider, and passes everything else to the vendor. The response is the vendor’s response, unchanged. One exception, on streams: Meadow sets stream_options.include_usage to true and removes the usage-only chunk unless you asked for it. The rules for routing, fallback, limits, headers, and streaming are in the Models section above.

Scope: models.

curl -sS "$MEADOW_BASE_URL/v1/chat/completions" \
-H "Authorization: Bearer $MEADOW_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"gemini-3.1-flash-lite-preview","max_tokens":16,
"messages":[{"role":"user","content":"Reply with the single word: meadow"}]}'

Streaming. With stream: true the response is text/event-stream. Each event is a data: line holding one chat.completion.chunk object, and the stream ends with data: [DONE]. The events are the vendor’s own, except the usage-only chunk, which Meadow removes unless you asked for it. Send stream_options: {"include_usage": true} to get token counts in the stream. A captured stream, with one long field shortened:

data: {"choices":[{"delta":{"content":"meadow","role":"assistant"},"index":0}],"created":1795472352,"id":"4LsEa6-uFay9","model":"gemini-3.1-flash-lite-preview","object":"chat.completion.chunk","usage":{"completion_tokens":2,"prompt_tokens":8,"total_tokens":10}}
data: {"choices":[{"delta":{"extra_content":{"google":{"thought_signature":"EqkBCqYB..."}},"role":"assistant"},"finish_reason":"stop","index":0}],"created":1795472352,"id":"4LsEa6-uFay9","model":"gemini-3.1-flash-lite-preview","object":"chat.completion.chunk","usage":{"completion_tokens":2,"prompt_tokens":8,"total_tokens":10}}
data: [DONE]

Authentication

AuthorizationBearer

A Meadow API key, sent as Authorization: Bearer mdw_.... The header x-api-key: mdw_... is accepted in its place.

Request

An OpenAI Chat Completions request, plus the optional Meadow fields models and provider. At most 16 MiB.

modelstringRequired

claude-haiku-4-5-20251001 lets Meadow pick the provider. anthropic/claude-haiku-4-5-20251001 pins it, the same as provider.only: [anthropic], and sends the rest of the id to that provider as written. Must not be empty or null.

messageslist of maps from strings to anyRequired
The conversation, in the OpenAI format. Required. Each entry must be an object. Passed through unchanged.
modelslist of stringsOptional

Fallback models, at most 3, tried in order after model has failed on every allowed provider. Each follows the same rules as model. Not sent to the vendor.

providerobjectOptional

Which model providers may serve a request, and in what order. Send it as provider on a model request, or store it on a project as routing.models. The field names follow OpenRouter’s provider object. A field set on the request replaces the same field from the project. Fields the request leaves out keep the project’s value. Every entry must be a model provider. An unknown field returns 400.

streambooleanOptionalDefaults to false

When true, the response is a server-sent event stream.

Response headers

x-meadow-providerstringOptional

The provider that answered, for example anthropic. Present on a 200 and on a vendor error that was passed through.

x-meadow-modelstringOptional
The vendor's own id for the model that answered.
x-meadow-attemptsintegerOptional
The number of vendors tried, including the one that answered. A value above 1 means a fallback happened.
x-meadow-routestringOptional

Why this vendor answered, in stable codes only. direct: <vendor> when the first vendor in the route served the call. fallback: <vendor> <reason> -> <vendor> when an earlier vendor failed, with the same reason codes as attempts[].reason in an error body. failed: ... on a vendor error passed through. It never carries vendor error text.

x-meadow-provider-auth-failedstringOptional

Comma-separated providers that rejected your stored credential during this request (HTTP 401 or 403, or a failed token exchange). Absent when none did. Replace the provider key for each one named.

x-meadow-abandoned-attemptsstringOptional

Comma-separated providers that received the request and may bill for it, but whose response was not used (a timeout or broken connection after the request was sent, or an unusable 200 body). Absent when there are none.

x-request-idstringOptional

Meadow’s id for this request, req_ and 24 hex characters. Every response carries it, including errors, streams and unknown routes. Every error in the Error schema repeats it as request_id. Quote it when you report a problem.

x-meadow-provider-request-idstringOptional

The request id of the vendor that answered, taken from its x-request-id, request-id, x-amzn-requestid, apim-request-id or x-generation-id header, the first one present in that order. Use it with that vendor’s support. Vendors tried before a fallback are not listed. Absent when the vendor sent none.

x-gemini-service-tierstringOptional

The service tier Google served this call on, returned as Google sent it, on a served call answered by google. standard, priority or flex. standard after a request with service_tier priority means Google downgraded it and bills it at the standard rate. Absent when another provider served the call or Google sent none.

x-amzn-bedrock-service-tierstringOptional

The service tier Bedrock served this call on, returned as Bedrock sent it, on a served call answered by bedrock. default, priority, flex or reserved. Absent when another provider served the call or Bedrock sent none.

Response

The vendor’s Chat Completions response, or its event stream when stream is true. Read x-meadow-provider to learn which vendor answered.

Errors

400
Bad Request Error
401
Unauthorized Error
402
Payment Required Error
403
Forbidden Error
404
Not Found Error
413
Content Too Large Error
422
Unprocessable Entity Error
429
Too Many Requests Error
500
Internal Server Error
502
Bad Gateway Error