Skip to main content

Overview

core-ai provides a hierarchy of error types that help you handle failures gracefully. For the complete API surface, see the errors reference.

Error Hierarchy

CoreAIError

Base error class for all core-ai errors.
Usage:
Check for the most specific error types first, then fall back to CoreAIError to catch any standardized library error.

ValidationError

Thrown when core-ai rejects invalid caller input or local request configuration before making a provider call.
When thrown:
  • Empty messages array
  • Empty input for embeddings
  • Empty prompt for image generation
  • Unsupported input modalities in user messages (UnsupportedInputModalityError)
  • Invalid local request configuration

AbortedError

Thrown when an operation is cancelled via an AbortSignal.

StreamAbortedError

Thrown when a streaming operation is aborted via an AbortSignal.
Usage:
StreamAbortedError applies to an in-flight stream. Validation errors such as empty messages throw ValidationError.

ProviderError

Error for provider API failures (network errors, API errors, rate limits, etc.). Discriminate with subclasses and instanceof. Transient failures (RateLimitError, ModelOverloadedError, ServiceUnavailableError) extend RetryableProviderError so you can retry with a single check.
Each provider wrap*Error helper decides which subclass to throw from SDK-specific fields (error types, status codes, message shapes). Typical order inside a wrapper: abort → context length → provider specials (quota, Azure capacity) → overload → rate limit → service unavailable / transient 5xx → generic ProviderError. HTTP 429 is not always RateLimitError. For example OpenAI insufficient_quota stays a non-retryable ProviderError, while Azure NoCapacity maps to ModelOverloadedError. Provider-specific quirks stay in the provider package (Azure "Backend error." as unavailable, Vertex RESOURCE_EXHAUSTED as a rate limit). Usage:
Subclass details:
  • ContextLengthExceededError — optional maxTokens / actualTokens when token counts are parseable (string-length limits are classified without inventing token metadata)
  • RateLimitError — optional retryAfterSeconds from Retry-After, Azure retry-after-ms, or an HTTP-date Retry-After value
  • ModelOverloadedError — capacity / high-demand signal (wins over generic 5xx unavailable; Azure NoCapacity on 429 included)
  • ServiceUnavailableError — temporary unavailability, including HTTP 500/502/503/504

Structured Output Errors

Errors specific to structured output generation (generateObject and streamObject).

StructuredOutputError

Base class for all structured output errors.
Properties:
  • rawOutput: The raw text output from the model (if available)
  • provider: Which provider was used
  • statusCode: HTTP status code (if applicable)
  • cause: Underlying error that caused this error

StructuredOutputNoObjectGeneratedError

Thrown when the model doesn’t generate any structured output.
When thrown:
  • Model responds with regular text instead of structured output
  • Tool call for structured output was not made
  • Finish reason is not ‘stop’ or ‘tool-calls’

StructuredOutputParseError

Thrown when the model’s output cannot be parsed as JSON.
When thrown:
  • Model output is not valid JSON
  • JSON syntax errors in model output
  • Malformed structured output

StructuredOutputValidationError

Thrown when the model’s output doesn’t match the Zod schema.
Properties:
  • issues: Array of human-readable validation error messages
When thrown:
  • Missing required fields
  • Wrong data types
  • Values that don’t match schema constraints
  • Invalid enum values

Complete Error Handling Example

Retry Strategies

Exponential Backoff

Circuit Breaker

Validation Best Practices

Validate inputs before sending: Check for empty messages, validate file sizes, and ensure proper content types before making API calls to avoid unnecessary errors.

Logging Errors

Log error details for debugging: Include provider, model, status codes, and raw output when available to help diagnose issues.

Next Steps

  • Learn about Providers for provider-specific behavior
  • Explore Configuration for controlling generation
  • Understand Messages for building conversations