Overview
core-ai exports helpers for schema conversion, portable tool names, assistant message construction, stream creation, model middleware wrapping, reasoning effort clamping, and provider-specific metadata access.toJsonSchema()
Return the JSON Schema that core-ai sends for a schema. It applies the same conversion core-ai uses for tool parameters and structured output schemas.- A Standard Schema is converted to the JSON Schema of its input type (draft 2020-12).
- Plain JSON Schema is returned unchanged.
- A schema that cannot be converted throws a
ValidationError.
toPortableToolName()
Return a tool name every provider accepts, for tool names you do not control, such as MCP tool names.- A name of letters, digits, underscores, and dashes that starts with a letter or underscore and has at most 128 characters is returned unchanged.
- Any other name has each other character replaced with
_, gets a leading_unless it starts with a letter or underscore, and is truncated to 128 characters. Characters are replaced per UTF-16 code unit without Unicode normalization, so🙂becomes__.
a.b and a:b to a_b. Check that the converted names in your tool catalog stay unique, and rename a tool yourself where they don’t.
The model calls the tool by the converted name, so keep a map from the converted name to the original one and translate tool calls back. Discovered tools must have such a name; see Tool snapshots.
clampReasoningEffort()
Clamp a requested reasoning effort to the nearest value in a model’s supported effort list.ChatModel.capabilities or a provider get*ModelCapabilities helper before calling generate / stream.
resultToMessage()
Convert aGenerateResult into an AssistantMessage so you can continue a conversation with the assistant’s full response parts.
Options
includeReasoning: false if you want to drop reasoning parts when reusing the message.
assistantMessage()
Create a simple assistant message from plain text.getProviderMetadata()
Read provider-specific metadata from reasoning parts in a type-safe way.'anthropic', 'google', or 'openai'.
createChatStream()
Create aChatStream from an async iterable of StreamEvents. This is useful when building custom providers or testing.
source parameter accepts an AsyncIterable<StreamEvent> or a () => Promise<AsyncIterable<StreamEvent>> factory function.
Options
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. Pass the request the stream answers so that, with toolDiscovery, calls to the discovery tool arrive as one tool-discovery-call event.
A custom ChatModel that builds its GenerateResult itself passes it through finalizeGenerateResult(result, options) for the same treatment.
createObjectStream()
Create anObjectStream from an async iterable of ObjectStreamEvents.
createChatStream, this accepts an optional { signal: AbortSignal } options object.
wrapChatModel()
Wrap aChatModel with middleware to add cross-cutting behavior like logging, validation, or tracing.
ChatModel with the same interface. See the Middleware concept page for details on writing and composing middleware.
wrapEmbeddingModel()
Wrap anEmbeddingModel with middleware.
wrapImageModel()
Wrap anImageModel 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 toRecord<string, unknown>. Returns an empty object if the input is not an object.
safeParseJsonObject()
Safely parse a JSON string into an object. Returnsundefined if parsing fails or the result is not an object.