API reference
Every public function and type, with signatures generated from the source.
Each page documents one symbol: what it does, when to reach for it, and its exact signature.
Signatures here are extracted from Sources/AI on every build, so they cannot
drift from the code. The prose around them is written by hand. Everything else
in the docs teaches a concept or walks a task; this section is for looking
something up.
Functions
Text
generateText
Generate text and run the tool loop to completion, returning one finished result.
streamText
Stream text, reasoning, tool calls, and results as they arrive.
streamTextDeltas
A minimal text-only stream, without the tool loop.
Structured output
generateEnum
Pick exactly one value from a fixed set.
generateJSON
Generate free-form JSON with no schema.
generateObject
Generate a typed value validated against a JSON schema.
generateObjectArray
Generate a typed array, validating each element against a schema.
streamObject
Stream a structured object as its fields fill in.
Embeddings and ranking
cosineSimilarity
Cosine similarity between two vectors.
embed
Embed a single string into a vector.
embedMany
Embed many strings, batching automatically.
rerank
Reorder candidate documents against a query.
Images, audio, and video
detectAudioMediaType
Identify an audio format from its bytes.
detectImageMediaType
Read an image's media type from its magic bytes.
generateImage
Generate or edit images.
generateSpeech
Synthesize speech from text.
generateVideo
Generate video, polling until the job completes.
streamTranscribe
Transcribe live audio as it arrives.
transcribe
Transcribe an audio file to text.
Loop control
hasToolCall
Stop once a named tool has been called.
isLoopFinished
Stop when the model stops asking for tools.
isStepCount
Stop on an exact step number.
stepCountIs
Stop the tool loop after a number of steps.
Context
filterActiveTools
Narrow a tool list to a named subset.
pruneMessages
Drop old tool traffic and reasoning from a history.
Middleware and providers
customProvider
Build a provider from your own model lookups.
wrapEmbeddingModel
Wrap an embedding model with middleware.
wrapImageModel
Wrap an image model with middleware.
wrapLanguageModel
Wrap a language model with middleware.
wrapProvider
Wrap every model a provider produces.
UI and transports
consumeStream
Drain a stream to completion, discarding output.
convertToModelMessages
Convert UI messages to model messages.
lastAssistantMessageIsCompleteWithApprovalResponses
Check whether pending tool approvals have been answered.
lastAssistantMessageIsCompleteWithToolCalls
Check whether the last assistant turn has all its tool results.
readUIMessageStream
Turn a UI message stream into message snapshots.
safeValidateUIMessages
Validate UI messages without throwing.
smoothStream
Re-chunk a text stream by word or line.
validateUIMessages
Validate incoming UI messages, throwing on bad input.
MCP
detectToolDrift
Compare tool fingerprints against an approved baseline.
fingerprintTools
Snapshot tool definitions so changes can be detected.
Audio buffers
decodePCM16
Decode 16-bit PCM into samples.
encodePCM16
Encode samples as 16-bit PCM.
resampleAudio
Resample PCM audio between sample rates.
Utilities
createIdGenerator
Build an id generator with a prefix and alphabet.
generateId
Generate an id in the SDK default format.
getRealtimeToolDefinitions
Convert tools into realtime session definitions.
uploadFile
Upload a file to a provider and get a reference back.
Types
The types you construct or inspect directly. Everything else in the public API is plumbing these hand back to you.
Building blocks
Agent
A model, its instructions, and its tools bundled into one reusable object.
ContentPart
One piece of a message: text, an image, a file, a tool call, a result, or reasoning.
Message
One turn in a conversation: a role and the content parts that make it up.
Tool
A function the model can call, with a name, a description, and a parameter schema.
Results
GenerateTextResult
Everything one finished generateText call produced.
StreamTextResult
A live stream of one generation, plus the finished result once it lands.
Configuration
CompactedContext
The structured summary compaction produces: goal, decisions, dead ends, and facts.
Compaction
Automatic context compaction: when to compress a conversation, and how hard.
GenerationTimeout
Deadlines for a generation: total wall clock, and how long a silent stream may stall.
StopCondition
When the tool loop should stop taking another step.
TelemetrySettings
What the SDK records about a run, and where it sends it.
ToolApprovalPolicy
Which tool calls need a human yes before they run.
ToolChoice
Whether the model may call tools, must call one, or must call a specific one.
UI
Values
JSONValue
A JSON value: object, array, string, number, boolean, or null.
MCPClient
A connection to an MCP server, and the tools it exposes.
Conventions
Swift argument labels are shown as written, so _ means the parameter is
called positionally. A parameter with no default is required; everything
else can be omitted.
any LanguageModel and any AIToolProtocol appear throughout because the SDK
takes existentials rather than generics on those positions, which is what lets
you swap a provider without changing a call site.
Type pages list stored properties, initializers, and methods, including any
added in a public extension. Computed properties and protocol conformances
are left out.
For the error cases these functions throw, see the errors reference.