Skip to main content

Overview

core-ai provides a hierarchy of error classes for handling different types of failures when working with language models. All errors extend from CoreAIError and provide structured error information.

Error Hierarchy

CoreAIError

Base error class for all core-ai errors.

ValidationError

Thrown when core-ai rejects invalid caller input or local request configuration before calling a provider. This includes schemas core-ai cannot use: a tool’s parameters or a structured output schema that cannot be converted to JSON Schema, a Standard Schema without JSON Schema support, or a value that is neither kind of schema. The message names the tool or schema, and cause holds the schema library’s conversion error when there is one. With tool discovery in use, it also covers the discovery rules:
  • A tool or discovered tool named core_ai_discover_tools or core_ai_deferred_tools_placeholder, which core-ai reserves.
  • A discovery call without a tool-discovery message answering it before the next user or assistant message.
  • A tool-discovery message that answers no discovery call of the preceding assistant message, or answers a call that is already answered.
  • A discovery call whose id another call of the same assistant message also uses.
  • A tool-discovery message with both tools and a note.
  • A tool-discovery message that lists the same tool name more than once.
  • A discovered tool whose name some provider rejects.
  • A discovered tool whose parameters are not plain JSON Schema, or not an object schema with type: "object" at the root.
  • A toolChoice that names a tool outside tools.

Properties

string
Error message describing what went wrong.
unknown
Optional underlying cause of the error (e.g., network error, API error).
string | undefined
Optional provider context when the error can be attributed to a provider.
string
Error name, always 'ValidationError'.

Example

ToolSchemaStrictnessError

Thrown before the provider request when a strict: true tool is unsupported, exceeds a provider limit, or has a schema outside the strict-capable contract. Extends ValidationError.

Properties

string
Provider that rejected the local configuration.
string
Model ID used for capability resolution.
readonly string[]
Tools that requested strict enforcement.
ToolSchemaStrictnessErrorReason
Machine-readable failure category. unsupported means the model does not support strict tool schemas. limit-exceeded means the request contains more strict tools than the provider allows. union-limit-exceeded means the strict tools have more union-typed parameters than the provider allows. invalid-schema means a strict tool’s schema is outside the strict-capable contract.
number | undefined
Provider limit when reason is limit-exceeded.
number | undefined
Provider limit when reason is union-limit-exceeded.
number | undefined
Union-typed parameters across the request’s strict tools when reason is union-limit-exceeded. toolNames then lists the strict tools that have any.
readonly StrictToolSchemaViolation[] | undefined
When reason is invalid-schema, one entry per offending schema path with a fix hint. Also available directly through the exported getStrictToolSchemaViolations() helper.

Example

See Strict tool schemas for the strict-capable schema contract and provider support. This error validates request configuration. It does not report invalid arguments returned by a model. Check those with validateToolCall() for Standard Schema tools, or validate them yourself for plain JSON Schema tools, before execution.

UnsupportedInputModalityError

Thrown when user messages include content parts the model does not accept, before the provider call. Extends ValidationError.

Properties

readonly string[]
Modalities present in the user messages.
readonly string[]
Modalities from model.capabilities.modalities.input.
readonly string[]
Requested modalities missing from the model capabilities.

Example

UnsupportedSystemMessagePlacementError

Thrown when a system message sits where the model does not accept one, before the provider call. The rule comes from model.capabilities.messages.systemPlacement. Extends ValidationError.

Properties

'leading' | 'before-reply' | 'anywhere'
The model’s rule for system messages after the first non-system message.
readonly SystemMessagePlacementIssue[]
Each offending system message, by its index in messages, with the rule it breaks. The error message includes a fix for each one.
To check a conversation without sending it, call the exported getSystemMessagePlacementIssues(messages, placement) helper. See System message placement.

Example

AbortedError

Thrown when an operation is cancelled via an AbortSignal.

StreamAbortedError

Thrown when a streaming operation is aborted via an AbortSignal.

Example

ProviderError

Error for provider/API failures. Discriminate with subclasses and instanceof. Transient failures extend RetryableProviderError.

Properties

string
The provider that threw the error (e.g., 'openai', 'anthropic').
number | undefined
HTTP status code if available (e.g., 404, 500, 429).

Classified subclasses

Each provider wrap*Error helper decides which subclass to throw from SDK-specific fields (error types, status codes, message shapes). Use instanceof to branch in application code — statusCode is for logging and debugging, not for retry decisions. HTTP 429 is not always RateLimitError: billing quota (for example OpenAI insufficient_quota) remains a non-retryable ProviderError, while some capacity signals (for example Azure NoCapacity) map to ModelOverloadedError. RateLimitError.retryAfterSeconds may come from Retry-After, Azure retry-after-ms, or an HTTP-date Retry-After header.

Example

StructuredOutputError

Base class for errors related to structured output generation (objects).

Properties

string | undefined
The raw output from the model that failed to parse/validate.

Example

StructuredOutputNoObjectGeneratedError

Thrown when the model doesn’t generate an object at all.

Example

StructuredOutputParseError

Thrown when the model’s output cannot be parsed as JSON.

Example

StructuredOutputValidationError

Thrown when the model’s output doesn’t match a Standard Schema passed as schema to generateObject() or streamObject(). Plain JSON Schema output is never validated by core-ai, so it never raises this error.

Properties

string[]
Validation messages from the schema library, each formatted as path.to.key: message. Issues at the root use <root> as the path.

Example

Error Handling Patterns

Comprehensive Error Handling

Retry Logic with Error Handling

Logging and Monitoring

Graceful Degradation

Best Practices

Always check for specific error types before general ones. Use instanceof checks in order from most specific to least specific.
Don’t retry validation errors (StructuredOutputValidationError) - they indicate a schema mismatch that won’t be fixed by retrying.
Gate retries on RetryableProviderError — the umbrella for transient failures (rate limits, overload, and transient 5xx) — with exponential backoff. Don’t treat every HTTP 429 as retryable: billing quota errors surface as a non-retryable ProviderError.
The rawOutput property in structured output errors is useful for debugging what the model actually generated.
Log error details including provider, status codes, and causes for debugging and monitoring.