AI Gateway Embeddings
Generate vector embeddings for semantic search, similarity matching, and retrieval-augmented generation (RAG).
To see which models AI Gateway supports for embeddings, use the Embedding filter at the AI Gateway Models page.
Use AI SDK 7, the AI SDK for Python beta, TanStack AI, or the OpenAI-compatible Embeddings API. Embeddings use /v1/embeddings; Chat Completions, Messages, and Responses don't accept embedding requests. Set AI_GATEWAY_API_KEY before running the examples.
For SDK options and result types, see AI SDK embeddings and Python embeddings.
import { embed } from 'ai';
export async function GET() {
const result = await embed({
model: 'openai/text-embedding-3-small',
value: 'Sunny day at the beach',
});
return Response.json(result);
}import asyncio
import ai
async def main():
result = await ai.ops.embed(
ai.get_model('openai/text-embedding-3-small'),
["Sunny day at the beach"],
)
print(result.value)
asyncio.run(main())const response = await fetch('https://ai-gateway.vercel.sh/v1/embeddings', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.AI_GATEWAY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'openai/text-embedding-3-small',
input: ['Sunny day at the beach'],
}),
});
if (!response.ok) throw new Error(await response.text());
console.log(await response.json());import json
import os
import urllib.request
request = urllib.request.Request(
"https://ai-gateway.vercel.sh/v1/embeddings",
data=json.dumps({"model": "openai/text-embedding-3-small", "input": ["Sunny day at the beach"]}).encode(),
headers={
'Authorization': "Bearer " + os.environ["AI_GATEWAY_API_KEY"],
'Content-Type': "application/json"
},
)
with urllib.request.urlopen(request) as response:
print(json.load(response))curl --fail-with-body https://ai-gateway.vercel.sh/v1/embeddings \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/text-embedding-3-small",
"input": [
"Sunny day at the beach"
]
}'import { embedMany } from 'ai';
export async function GET() {
const result = await embedMany({
model: 'openai/text-embedding-3-small',
values: ['Sunny day at the beach', 'Cloudy city skyline'],
});
return Response.json(result);
}import asyncio
import ai
async def main():
result = await ai.ops.embed(
ai.get_model('openai/text-embedding-3-small'),
["Sunny day at the beach", "Cloudy city skyline"],
)
print(result.value)
asyncio.run(main())const response = await fetch('https://ai-gateway.vercel.sh/v1/embeddings', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.AI_GATEWAY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'openai/text-embedding-3-small',
input: ['Sunny day at the beach', 'Cloudy city skyline'],
}),
});
if (!response.ok) throw new Error(await response.text());
console.log(await response.json());import json
import os
import urllib.request
request = urllib.request.Request(
"https://ai-gateway.vercel.sh/v1/embeddings",
data=json.dumps({"model": "openai/text-embedding-3-small", "input": ["Sunny day at the beach", "Cloudy city skyline"]}).encode(),
headers={
'Authorization': "Bearer " + os.environ["AI_GATEWAY_API_KEY"],
'Content-Type': "application/json"
},
)
with urllib.request.urlopen(request) as response:
print(json.load(response))curl --fail-with-body https://ai-gateway.vercel.sh/v1/embeddings \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/text-embedding-3-small",
"input": [
"Sunny day at the beach",
"Cloudy city skyline"
]
}'Alternatively, if you're using the Gateway provider instance, specify embedding models with gateway.textEmbeddingModel(...).
import { embed } from 'ai';
import { gateway } from '@ai-sdk/gateway';
export async function GET() {
const result = await embed({
model: gateway.textEmbeddingModel('openai/text-embedding-3-small'),
value: 'Sunny day at the beach',
});
return Response.json(result);
}Use TanStack AI's embed function with vercelGatewayEmbedding from @tanstack/ai-vercel-gateway to generate embeddings. The adapter reads AI_GATEWAY_API_KEY, or uses VERCEL_OIDC_TOKEN when the API key is absent.
Follow the TanStack AI setup guide for installation and authentication, then use the embedding example. See the TanStack AI adapter reference for the API and result shape.
Was this helpful?