Skip to main content

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.
Use it when you store or measure schemas, for example to persist tool definitions or estimate token counts. You get the same JSON Schema core-ai hands to provider adapters, which apply only semantics-preserving provider transforms on top, such as the normalization for strict tool schemas. Each call returns a copy, so changing it does not affect what core-ai sends.

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 __.
The result depends only on the name you pass, so it stays the same across requests and releases, which keeps conversation histories and the prompt cache valid. Different names can convert to the same one, such as 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.
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. 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 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.