Skip to main content
Swift SDK / Inference MossInference is a Swift client for Moss Inference, which searches your indexes in the cloud and answers in OpenAI’s chat completions shape. chat.completions.create returns the whole reply, and createStreaming streams it.
Requires the Moss Swift package 0.12.0 or later, installed as in Install.

Create a client

The project id and key come from the Moss portal. Build one client and reuse it for every call. The initializer throws InferenceError for an argument it refuses.

Get a reply

completion.requestId is the response’s X-Moss-Request-Id, and so is stream.requestId on a stream. Include it when you report a problem. Both are nil when the response has none.

Stream a reply

To stop early, call stream.cancel(): it closes the connection, which stops the model, and ends the loop without an error. Only the last chunk carries usage, and it has no choices, which is why the loop reads choices.first.

Errors

A failed call throws InferenceError, from the for try await loop once a stream has started. status is the HTTP status of a refused request, type names the failure, and message describes it. code is the error code Moss Inference sent, such as "rate_limit_exceeded", requestId is the response’s X-Moss-Request-Id, and retryAfter is the wait in seconds that a Retry-After header asks for. A field the failure lacks is nil. The SDK sets these types itself, with status nil: Every other error reports what Moss Inference sent. See Errors, and Limits for a 429.

Tool calls

Pass your functions in tools. When the model calls them, append its message unchanged with ChatMessage(message), then one .tool(callId:content:) per call, and call again, as in Tool calls:
ChatMessage(message) keeps every field of each tool call. createStreaming takes the same tools and streams each call in pieces, in delta.toolCalls. create returns the calls joined, so use it for a reply you send back.

MCP servers

With an McpServer in tools, Moss Inference calls that server’s tools itself, in the same call:
McpServer also takes allowedTools and transport, .streamableHTTP or .sse. Steps arrive in chunk.toolStep and completion.toolSteps, with camelCase fields such as isError, and MCP failures throw InferenceError with status, type, code and retryAfter, as in MCP servers.

Options

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