Implementation:Openai Openai node Embeddings Resource
| Knowledge Sources | |
|---|---|
| Domains | SDK, Embeddings |
| Last Updated | 2026-02-15 12:00 GMT |
Overview
The Embeddings resource class provides the create method for generating embedding vectors from input text via the OpenAI Embeddings API.
Description
The Embeddings class extends APIResource and exposes a single create method that sends a POST request to the /embeddings endpoint. The method includes a notable performance optimization: when the user does not explicitly specify an encoding_format, the SDK automatically defaults to base64 encoding for the API request. The base64-encoded response is then transparently decoded back to Float32Array values before being returned, which reduces network transfer size while maintaining the default float array output that callers expect.
The response is wrapped in a CreateEmbeddingResponse interface containing an array of Embedding objects (each with an embedding vector, index, and object type), the model name, and usage statistics including prompt_tokens and total_tokens.
The EmbeddingCreateParams interface accepts input (a string, array of strings, array of tokens, or array of token arrays), model (supporting text-embedding-ada-002, text-embedding-3-small, and text-embedding-3-large), and optional parameters for dimensions, encoding_format, and user. The maximum input is 8192 tokens per input and 300,000 tokens across all inputs in a single request.
Usage
Use the Embeddings resource when you need to convert text into vector representations for similarity search, clustering, classification, or retrieval-augmented generation (RAG). Access it via client.embeddings.create() with the input text and model selection.
Code Reference
Source Location
- Repository: openai-node
- File: src/resources/embeddings.ts
Signature
export class Embeddings extends APIResource {
create(body: EmbeddingCreateParams, options?: RequestOptions): APIPromise<CreateEmbeddingResponse>;
}
export interface CreateEmbeddingResponse {
data: Array<Embedding>;
model: string;
object: 'list';
usage: CreateEmbeddingResponse.Usage;
}
export interface Embedding {
embedding: Array<number>;
index: number;
object: 'embedding';
}
export type EmbeddingModel =
| 'text-embedding-ada-002'
| 'text-embedding-3-small'
| 'text-embedding-3-large';
export interface EmbeddingCreateParams {
input: string | Array<string> | Array<number> | Array<Array<number>>;
model: (string & {}) | EmbeddingModel;
dimensions?: number;
encoding_format?: 'float' | 'base64';
user?: string;
}
Import
import OpenAI from 'openai';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| input | Array<string> | Array<number> | Array<Array<number>> | Yes | Text or token arrays to embed (max 8192 tokens per input, 300k total) |
| model | EmbeddingModel | Yes | Model ID (e.g., text-embedding-3-small, text-embedding-3-large, text-embedding-ada-002)
|
| dimensions | number |
No | Output embedding dimensions (only for text-embedding-3 and later)
|
| encoding_format | 'base64' | No | Response encoding format (SDK defaults to base64 internally for performance) |
| user | string |
No | Unique end-user identifier for abuse monitoring |
Outputs
| Name | Type | Description |
|---|---|---|
| data | Array<Embedding> |
List of embedding objects, each with a float vector, index, and object type |
| model | string |
The name of the model used to generate the embeddings |
| object | 'list' |
Always list
|
| usage.prompt_tokens | number |
Number of tokens used by the prompt |
| usage.total_tokens | number |
Total number of tokens consumed |
Usage Examples
import OpenAI from 'openai';
const client = new OpenAI();
// Create a single embedding
const response = await client.embeddings.create({
input: 'The quick brown fox jumped over the lazy dog',
model: 'text-embedding-3-small',
});
console.log('Embedding dimensions:', response.data[0].embedding.length);
console.log('Tokens used:', response.usage.total_tokens);
// Create multiple embeddings in one request
const batchResponse = await client.embeddings.create({
input: [
'First document text',
'Second document text',
'Third document text',
],
model: 'text-embedding-3-small',
dimensions: 256, // reduce dimensions for efficiency
});
for (const embedding of batchResponse.data) {
console.log(`Embedding ${embedding.index}: ${embedding.embedding.length} dims`);
}