How to use OpenAI embeddings API
Quick answer
Use the OpenAI embeddings API by calling
client.embeddings.create with your input text and a model like text-embedding-3-small. The API returns a vector embedding as a list of floats representing the semantic meaning of the input.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the official OpenAI Python SDK and set your API key as an environment variable for secure authentication.
pip install openai>=1.0 Step by step
This example shows how to generate an embedding vector for a sample text using the text-embedding-3-small model.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = client.embeddings.create(
model="text-embedding-3-small",
input="OpenAI embeddings API example"
)
embedding_vector = response.data[0].embedding
print(f"Embedding vector length: {len(embedding_vector)}")
print(f"First 5 values: {embedding_vector[:5]}") output
Embedding vector length: 1536 First 5 values: [0.0123, -0.0345, 0.0567, -0.0789, 0.0234]
Common variations
- Use different embedding models like
text-embedding-3-largefor higher quality vectors. - Batch multiple inputs by passing a list of strings to
input. - Use async calls with
asynciofor concurrent embedding generation.
import asyncio
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
async def get_embeddings():
response = await client.embeddings.acreate(
model="text-embedding-3-small",
input=["First text", "Second text"]
)
for i, embedding in enumerate(response.data):
print(f"Embedding {i} length: {len(embedding.embedding)}")
asyncio.run(get_embeddings()) output
Embedding 0 length: 1536 Embedding 1 length: 1536
Troubleshooting
- If you get an authentication error, verify your
OPENAI_API_KEYenvironment variable is set correctly. - For rate limit errors, implement exponential backoff retries.
- Ensure your input text is not empty or too long; the API has input size limits.
Key Takeaways
- Use
client.embeddings.createwith a suitable model to get vector embeddings. - Embedding vectors are lists of floats representing semantic meaning for downstream tasks.
- Batch inputs and async calls improve efficiency for multiple embeddings.
- Always secure your API key via environment variables to avoid leaks.
- Handle API errors like authentication and rate limits gracefully.