Skip to main content
MossInference is the Swift client for Moss Inference. It searches your indexes in the cloud and answers in the shape of OpenAI’s Responses API. responses.create returns the whole response, and createStreaming streams its events.

Install

MossInference needs the Moss Swift package 0.13.0 or later. For Xcode and 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. The initializer throws InferenceError for an argument it refuses. The examples below use this client. Every call is async and throws, so each one takes try await.

Get a response

response.outputText joins the text of the response’s messages. response.output holds its OutputItems: .message, .functionCall, .mcpCall, or .other for any other type. Each one has its JSON in raw. response.requestId is the response’s X-Moss-Request-Id. A stream has the same stream.requestId. Include it when you report a problem. Both are nil when the response has none. Moss stores no response, so input holds the whole conversation. Pass a string, or an array of InputItems:
  • .system, .developer, .user and .assistant take the text of a message, such as .user("Hi").
  • .functionCallOutput(callId:output:) takes a tool’s result.
  • InputItem(item) sends back an output item of an earlier response.
  • .raw takes any other item as a JSON object.
See Conversations.

Stream a response

createStreaming takes the same arguments as create. Each ResponseStreamEvent 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 maxOutputTokens or a content filter cut the response short.
To stop early, call stream.cancel(). It closes the connection, which stops the model, and ends the loop without an error. 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 throws 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 apiKey:
The key goes in the X-Moss-Provider-Key header, never in the body. To use a key for one call only, pass apiKey to create or createStreaming. Bring your own key covers model names and billing.

Tool calls

Pass your functions in tools, as the flat function tools of OpenAI’s Responses API. Each call the model makes comes as a .functionCall item of output. Put the output items back into input with InputItem(item). Add one .functionCallOutput(callId:output:) per call, and call again until the response has no calls:
lookupOrder stands for your own code, and it returns a String. InputItem(item) sends the item’s 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 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 .functionCallOutput items for them.
McpServer also takes allowedTools, and transport as .streamableHTTP or .sse. 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. An McpCall has the call’s serverLabel, name and arguments. Once the call has ended, it also has its status, output or error, and durationMs. Send the items back in input with the rest of output. The model then sees the earlier tool results. MCP failures throw InferenceError with status, type, code and retryAfter. 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 throws InferenceError. Once a stream has started, it throws from the for try await loop. A response.failed event throws it too. A field the failure lacks is nil. The SDK sets these types itself, with status nil: Every other error carries what Moss Inference sent. Errors and limits lists every code. For a 429, see Limits.

Options

MossInference(...): create and createStreaming take the same labeled arguments, in this order: