Implementation:Openai Openai python Embedding Response Model
Appearance
| Knowledge Sources | |
|---|---|
| Domains | NLP, Embeddings, Data_Processing |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete typed response models for extracting embedding vectors and usage data provided by the OpenAI Python SDK.
Description
CreateEmbeddingResponse contains a list of Embedding objects, each with a float vector and position index. The Usage object tracks token consumption. When numpy is available and base64 encoding is used, vectors are automatically decoded to numpy arrays.
Usage
Access vectors via response.data[i].embedding. Use response.data[i].index to match embeddings to input order. Check response.usage for token consumption.
Code Reference
Source Location
- Repository: openai-python
- File: src/openai/types/create_embedding_response.py
- Lines: L1-33
- File: src/openai/types/embedding.py
- Lines: L1-25
Signature
class CreateEmbeddingResponse(BaseModel):
data: List[Embedding]
model: str
object: Literal["list"]
usage: Usage
class Usage(BaseModel):
prompt_tokens: int
total_tokens: int
class Embedding(BaseModel):
embedding: List[float]
index: int
object: Literal["embedding"]
Import
from openai.types import CreateEmbeddingResponse, Embedding
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| response | CreateEmbeddingResponse | Yes | API response from embeddings.create() |
Outputs
| Name | Type | Description |
|---|---|---|
| data[i].embedding | list[float] | Float vector of model output dimensions |
| data[i].index | int | Position matching input order |
| model | str | Model used for embedding |
| usage.prompt_tokens | int | Input tokens consumed |
| usage.total_tokens | int | Total tokens consumed |
Usage Examples
Extract Vectors
from openai import OpenAI
client = OpenAI()
response = client.embeddings.create(
input=["Hello world", "Goodbye world"],
model="text-embedding-3-small",
)
# Extract vectors in input order
vectors = [item.embedding for item in sorted(response.data, key=lambda x: x.index)]
print(f"Got {len(vectors)} vectors of dimension {len(vectors[0])}")
print(f"Tokens used: {response.usage.total_tokens}")
Compute Similarity
import numpy as np
def cosine_similarity(a, b):
a, b = np.array(a), np.array(b)
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
response = client.embeddings.create(
input=["I love programming", "Coding is my passion", "The weather is nice"],
model="text-embedding-3-small",
)
vecs = [d.embedding for d in response.data]
print(f"Similar: {cosine_similarity(vecs[0], vecs[1]):.3f}")
print(f"Different: {cosine_similarity(vecs[0], vecs[2]):.3f}")
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment