X Enterprises

fastify-x-ai

Vercel AI SDK plugin for unified access to AI providers — text generation, streaming, embeddings, structured output, and tool calling.

fastify-x-ai

A Fastify plugin wrapping the Vercel AI SDK for unified access to OpenAI, Anthropic, and Google AI providers. Exposes text generation, streaming, chat, embeddings, structured output, and tool calling through a single fastify.xai decorator.

Installation

npm install @xenterprises/fastify-x-ai ai

# Install provider SDKs as needed
npm install @ai-sdk/openai    # OpenAI / GPT models
npm install @ai-sdk/anthropic # Anthropic / Claude models
npm install @ai-sdk/google    # Google / Gemini models

Quick Start

import Fastify from "fastify";
import xAI from "@xenterprises/fastify-x-ai";

const fastify = Fastify();

await fastify.register(xAI, {
  defaultProvider: "openai",
  providers: {
    openai: { apiKey: process.env.OPENAI_API_KEY },
  },
});

// Simple completion
const text = await fastify.xai.complete("Write a haiku about coding");

// Chat endpoint
fastify.post("/chat", async (request, reply) => {
  const result = await fastify.xai.chat({ messages: request.body.messages });
  return { text: result.text };
});

Options

NameTypeDefaultRequiredDescription
activebooleantrueNoSet false to disable the plugin entirely
defaultProviderstring"openai"NoDefault provider: openai, anthropic, or google
defaultModelstringProvider defaultNoDefault model name (falls back to per-provider defaults)
defaultMaxTokensnumber4096NoDefault max tokens; must be a positive integer
defaultTemperaturenumber0.7NoDefault temperature; must be 0–2
providersobject{}NoPer-provider config objects
providers.openai.apiKeystringOPENAI_API_KEYNoOpenAI API key (falls back to env var)
providers.openai.baseURLstringNoCustom OpenAI-compatible endpoint
providers.anthropic.apiKeystringANTHROPIC_API_KEYNoAnthropic API key
providers.google.apiKeystringGOOGLE_API_KEYNoGoogle API key

Default Models

ProviderDefault Model
openaigpt-4o
anthropicclaude-sonnet-4-20250514
googlegemini-2.0-flash

Methods

All methods are available on fastify.xai.

  • generate — Full-control text generation with prompt, messages, tools, and per-call model overrides.
  • stream — Streaming text generation; returns an async-iterable textStream.
  • chat — Chat with conversation history; delegates to generate or stream.
  • complete — Convenience wrapper that returns result.text directly from a prompt string.
  • createEmbedding — Create single or batch embeddings; includes similarity() helper.
  • generateStructured — Generate structured output validated against a Zod schema.
  • getModel / raw — Get a raw model instance; access underlying AI SDK functions via fastify.xai.raw.

Error Reference

ErrorCause
xAI: Invalid defaultProvider '…'defaultProvider not one of openai, anthropic, google
xAI: defaultMaxTokens must be a positive numberdefaultMaxTokens ≤ 0 or not a number
xAI: defaultTemperature must be a number between 0 and 2defaultTemperature outside 0–2
xAI: 'ai' package is requiredai peer dependency not installed
xAI: Provider '…' not configured. Available: …Method called with a provider that has no API key
xAI generate: Either 'prompt' or 'messages' is requiredgenerate() called without input
xAI stream: Either 'prompt' or 'messages' is requiredstream() called without input
xAI chat: 'messages' is requiredchat() called without messages
xAI complete: 'prompt' is requiredcomplete() called with empty/missing prompt
xAI createEmbedding: Either 'text' or 'texts' is requiredcreateEmbedding() called without input
xAI generateStructured: 'prompt' is requiredgenerateStructured() called without prompt
xAI generateStructured: 'schema' is requiredgenerateStructured() called without schema

Environment Variables

VariableRequiredDescription
OPENAI_API_KEYIf using OpenAIAuto-detected if not passed via providers.openai.apiKey
ANTHROPIC_API_KEYIf using AnthropicAuto-detected if not passed via providers.anthropic.apiKey
GOOGLE_API_KEYIf using GoogleAuto-detected if not passed via providers.google.apiKey

Explicit config in providers takes precedence over environment variables.

How It Works

On registration the plugin validates options, dynamically imports the ai package, then attempts to load each configured provider SDK (@ai-sdk/openai, @ai-sdk/anthropic, @ai-sdk/google). A provider is initialized only when its API key is found — either in providers config or the corresponding environment variable; missing SDKs log a warning instead of failing. All public methods resolve the correct model via getModel, validate inputs, and delegate to the underlying AI SDK primitives (generateText, streamText, embed, embedMany). The decorated fastify.xai object is available to every route handler and plugin in scope.

AI Context

package: "@xenterprises/fastify-x-ai"
type: fastify-plugin
use-when: Unified AI text generation, streaming, embeddings, and structured output via Vercel AI SDK — supports OpenAI, Anthropic, Google
decorator: fastify.xai (generate, stream, chat, complete, createEmbedding, generateStructured, getModel, raw)
env: OPENAI_API_KEY, ANTHROPIC_API_KEY (optional), GOOGLE_API_KEY (optional)
defaults: openai/gpt-4o, anthropic/claude-sonnet-4-20250514, google/gemini-2.0-flash
peer-deps: ai, @ai-sdk/openai (and/or @ai-sdk/anthropic, @ai-sdk/google)
Copyright © 2026