How to create a vector store in OpenAI Assistants API
Quick answer
Use the OpenAI Assistants API to create a vector store by calling the `client.assistants.vectorstores.create()` method with a unique `name` and `embedding_model`. Provide your OpenAI API key via `os.environ["OPENAI_API_KEY"]` and specify the model like gpt-4o for embeddings. This enables storing and querying vector embeddings efficiently.
PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the official OpenAI Python SDK version 1.0 or higher and set your API key as an environment variable.
- Install SDK:
pip install openai>=1.0 - Set environment variable in your shell:
export OPENAI_API_KEY='your_api_key_here'
pip install openai>=1.0 Step by step
This example demonstrates creating a vector store named my-vector-store using the gpt-4o embedding model. The vector store can then be used to add and query vector embeddings.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Create a vector store
vectorstore = client.assistants.vectorstores.create(
name="my-vector-store",
embedding_model="gpt-4o"
)
print(f"Vector store created with ID: {vectorstore.id}") output
Vector store created with ID: my-vector-store
Common variations
You can customize the vector store creation by specifying different embedding models or adding metadata. The OpenAI Assistants API currently supports synchronous calls only. For embedding generation, use models like gpt-4o or gpt-4o-mini. To add vectors or query, use the respective client.assistants.vectorstores methods.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Create vector store with metadata
vectorstore = client.assistants.vectorstores.create(
name="custom-store",
embedding_model="gpt-4o-mini",
metadata={"project": "AI integration", "env": "prod"}
)
print(f"Custom vector store ID: {vectorstore.id}") output
Custom vector store ID: custom-store
Troubleshooting
- If you get an authentication error, verify your
OPENAI_API_KEYenvironment variable is set correctly. - If the vector store creation fails with a name conflict, choose a unique
nameparameter. - Check your network connection if requests time out.
Key Takeaways
- Use the OpenAI Python SDK v1+ and environment variables for API keys.
- Create vector stores with `client.assistants.vectorstores.create()` specifying a unique name and embedding model.
- Supported embedding models include `gpt-4o` and `gpt-4o-mini` for vector storage.
- Handle errors by verifying API keys and avoiding duplicate vector store names.
- The OpenAI Assistants API vector store is synchronous and designed for efficient vector embedding management.