Skip to main content

Overview

core-ai exports helpers for schema conversion, assistant message construction, stream creation, model middleware wrapping, reasoning effort clamping, and provider-specific metadata access.

zodSchemaToJsonSchema()

Convert a Zod schema to JSON Schema using Zod 4’s native conversion APIs.
Use this helper when you need a JSON Schema outside the built-in model and tool helpers.

clampReasoningEffort()

Clamp a requested reasoning effort to the nearest value in a model’s supported effort list.
Returns the original effort when the supported list is empty or already includes it. Use this with ChatModel.capabilities or a provider get*ModelCapabilities helper before calling generate / stream.

resultToMessage()

Convert a GenerateResult into an AssistantMessage so you can continue a conversation with the assistant’s full response parts.

Options

Set includeReasoning: false if you want to drop reasoning parts when reusing the message.

assistantMessage()

Create a simple assistant message from plain text.
This is equivalent to constructing an assistant message with a single text part.

getProviderMetadata()

Read provider-specific metadata from reasoning parts in a type-safe way.
The top-level key matches the provider name, such as 'anthropic', 'google', or 'openai'.

createChatStream()

Create a ChatStream from an async iterable of StreamEvents. This is useful when building custom providers or testing.
The source parameter accepts an AsyncIterable<StreamEvent> or a () => Promise<AsyncIterable<StreamEvent>> factory function.

Options

Pass signal to abort the stream. Pass mapError so in-band source failures become the same typed errors as a failed initial request. Already-typed core-ai errors pass through unchanged.

createObjectStream()

Create an ObjectStream from an async iterable of ObjectStreamEvents.
Like createChatStream, this accepts an optional { signal: AbortSignal } options object.

wrapChatModel()

Wrap a ChatModel with middleware to add cross-cutting behavior like logging, validation, or tracing.
Returns a new ChatModel with the same interface. See the Middleware concept page for details on writing and composing middleware.

wrapEmbeddingModel()

Wrap an EmbeddingModel with middleware.

wrapImageModel()

Wrap an ImageModel with middleware.

stripModelDateSuffix()

Remove a trailing -YYYYMMDD date suffix from a model ID. This is used internally for model capability lookups.

asObject()

Safely cast an unknown value to Record<string, unknown>. Returns an empty object if the input is not an object.

safeParseJsonObject()

Safely parse a JSON string into an object. Returns undefined if parsing fails or the result is not an object.