Skip to main content
MossInference is the Python client for Moss Inference. Its responses.create takes OpenAI’s Responses arguments, searches your indexes, and returns OpenAI’s field names.

Install

MossInference needs moss 1.22.0 or later. Earlier versions have chat.completions.create instead. See the changelog. For the rest of the SDK setup, see Install.

Create a client

Copy the project id and key from the Moss portal. Build one client and reuse it for every call. To close its connections, call await client.close() or use async with client:. The API is async only. The examples below use this client inside an async function. With MOSS_PROJECT_ID and MOSS_PROJECT_KEY set, from moss import inference gives you inference.responses.create with no client to build.

Get a response

response.output_text joins the text of the response’s messages. response.output holds its items. response.request_id is the response’s X-Moss-Request-Id. A stream has the same stream.request_id. Include it when you report a problem. Both are None when the response has none. Moss stores no response, so input holds the whole conversation. Pass a string, or a list of items such as {"role": "user", "content": "Hi"} and the output items of earlier responses. See Conversations.

Stream a response

Each event has OpenAI’s type, such as response.output_text.delta, and the fields of that type. The stream ends after one of two events:
  • response.completed, whose event.response is the whole response.
  • response.incomplete, when max_output_tokens or a content filter cut the response short.
await stream.get_final_response() reads the rest of the stream and returns that response. To stop early, break out of the loop. Leaving the async with stream: block closes the connection, which stops the model. Streaming lists every event.

Ground it in your indexes

Moss searches the faq index and adds what it finds to the prompt. The Quickstart creates that index. Without indexes, Moss does not search and the model answers from input alone. You can name at most 32. retrieve=False skips the search on a call that names indexes. Ground answers in your indexes covers the options and the cost.

Use a vendor model

Name a model from OpenAI, Anthropic or Google in model. The search of your indexes works the same for every model. Models lists them all.

On the Moss key

Without your own key, a listed vendor model runs on the Moss key and bills your credits. Routes shows who bills each call. A listed model that is not served right now raises InferenceError with code "model_not_enabled". Send moss/fastlane-1 or use your own key.

With your own key

Pass your OpenAI, Anthropic or Google key to a client as api_key:
The key goes in the X-Moss-Provider-Key header, never in the body. To use a key for one call only, pass api_key to responses.create. The response’s model is the name you sent. Bring your own key covers model names and billing.

Tool calls

Pass OpenAI function tools in tools. A function tool is flat, as in OpenAI’s Responses API. Each call the model makes comes as a ResponseFunctionToolCall item of output. Put the output items back into input as they are. Add one function_call_output item per call, and call again until the response has no calls:
lookup_order stands for your own code, and it returns text. Each item goes back as its raw JSON. It keeps every field the model sent, such as the thought signature Gemini needs in the next turn. Tool calling covers tool_choice, parallel_tool_calls and structured output.

MCP servers

An McpServer in tools names an MCP server. Moss Inference runs its tools for the model, so your code sends no function_call_output items for them.
Each call comes as an McpCall item. It arrives in response.output_item.added when it starts, and in response.output_item.done when it ends. The same items are in the response’s output. Send the items back in input with the rest of output. The model then sees the earlier tool results. MCP failures raise InferenceError with status, type, code and retry_after. A call that fails after a tool ran has code "failed_after_tool_call". The tool may already have changed data, so Moss Inference does not retry the call. Never replay a request that failed with failed_after_tool_call. MCP servers covers the server fields and every MCP error.

Errors

A failed call raises moss.inference.InferenceError. Once a stream has started, it raises from the async for loop. A response.failed event raises it too. A field the failure lacks is None. The SDK sets these types itself, with status None: Every other error carries what Moss Inference sent. Errors and limits lists every code. For a 429, see Limits.

Options

MossInference(project_id, project_key, ...): responses.create(...) takes keyword arguments only: