Overview
Thewrap() function automatically detects your AI client type and applies the appropriate instrumentation wrapper. All API calls made through the wrapped client are automatically traced and sent to ZeroEval.
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 AI client instance to wrap. Currently supports:
- OpenAI clients (from
openaipackage) - Vercel AI SDK module (from
aipackage)
Returns
A wrapped version of your client that preserves all original type information and functionality while adding automatic tracing.
Auto-Detection Logic
wrap() uses runtime type detection to determine which wrapper to apply:
- OpenAI Detection: Checks for
chat.completions,embeddings.create, and constructor name - Vercel AI Detection: Checks for functions like
generateText,streamText,generateObject,embed - Error on Unknown: Throws a descriptive error if the client type isn’t recognized
Type Preservation
The wrapped client maintains full TypeScript type information from the original client. You can use it exactly as you would use the unwrapped version:Double-Wrap Protection
Callingwrap() multiple times on the same client is safe. The function detects already-wrapped clients and returns them without re-wrapping:
Error Handling
If you pass an unsupported client type,wrap() throws an error with guidance:
Automatic Initialization
If you haven’t calledze.init() but have set ZEROEVAL_API_KEY in your environment, wrap() will automatically initialize the SDK:
Related Functions
wrapOpenAI()- OpenAI-specific wrapperwrapVercelAI()- Vercel AI SDK-specific wrapperze.init()- SDK initialization