Overview
wrapVercelAI() wraps the Vercel AI SDK module to automatically trace all AI operations. The wrapper intercepts SDK functions like generateText, streamText, generateObject, and embed, adding observability without changing your code.
If ze.init() hasn’t been called and ZEROEVAL_API_KEY is set in your environment, the SDK will automatically initialize itself.
Type Signature
Parameters
The Vercel AI SDK module exports (typically
import * as ai from 'ai').Returns
A wrapped version of the AI SDK with all supported functions instrumented for tracing.
Traced Functions
Text Generation
Functions:generateText(), streamText()
Traces text generation with:
- Input prompt or messages
- Generated text output
- Token usage metrics
- Throughput calculation
- Streaming metrics (latency to first token)
- Tool call information
Object Generation
Functions:generateObject(), streamObject()
Traces structured object generation with:
- Input prompt and schema
- Generated object
- Token usage metrics
Embeddings
Functions:embed(), embedMany()
Traces embedding generation with:
- Input text
- Number of embeddings generated
- Token usage metrics
Additional Functions
Functions:generateImage(), transcribe(), generateSpeech()
Traces image generation, audio transcription, and speech synthesis operations.
Vercel AI SDK Integration
The wrapper creates traced versions of SDK functions while preserving all original functionality:Streaming Support
The wrapper fully supports streaming responses fromstreamText() and streamObject():
- Time to first token (latency)
- Chunk count
- Full accumulated text
- Token usage from stream metadata
Metadata Extraction
The wrapper processes ZeroEval metadata embedded in prompts or system messages:- Extracts metadata from HTML comments
- Interpolates template variables
- Removes metadata before passing to the AI SDK
- Attaches metadata to the trace span
Double-Wrap Protection
Wrapping an already-wrapped module is safe and returns the existing wrapper:Error Tracing
Errors from AI SDK operations are automatically captured:Span Attributes
Each traced operation includes:Set to
"vercel-ai-sdk"Operation kind:
"llm", "embedding", "image", "speech", or "transcription"Set to
"vercel-ai-sdk"The model ID or provider-specific identifier
Messages array (for message-based generation)
Set to
true for streamText() and streamObject()Temperature parameter (if provided)
Max tokens parameter (if provided)
Max agent steps (if provided)
Number of tools provided
Prompt tokens consumed
Completion tokens generated
Characters per second
Time to first token (streaming only)
Number of chunks received (streaming only)
Extracted ZeroEval metadata (task, prompt_version_id, variables)
Supported Response Types
The wrapper handles all Vercel AI SDK response types:- Non-streaming: Traces complete response with usage metrics
- Streaming: Wraps
textStreamandfullStreamiterators - Object generation: Captures structured output from schema validation
- Embeddings: Records embedding count and dimensions
Related Functions
wrap()- Auto-detect and wrap any supported clientwrapOpenAI()- OpenAI-specific wrapper