Skip to main content

Method Signature

Parameters

Union[str, List[str], List[int], List[List[int]]]
required
Input text to embed, encoded as a string or array of tokens. To embed multiple inputs in a single request, pass an array of strings or array of token arrays.Constraints:
  • Must not exceed the max input tokens for the model (8192 tokens for all embedding models)
  • Cannot be an empty string
  • Any array must be 2048 dimensions or less
  • Maximum of 300,000 tokens summed across all inputs in a single request
Union[str, EmbeddingModel]
required
ID of the model to use. You can use the List models API to see all available models, or see the Model overview for descriptions.Popular models:
  • text-embedding-3-small
  • text-embedding-3-large
  • text-embedding-ada-002
int
The number of dimensions the resulting output embeddings should have. Only supported in text-embedding-3 and later models.
Literal['float', 'base64']
default:"base64"
The format to return the embeddings in. Can be either float or base64.Note: The SDK automatically decodes base64 embeddings to float arrays for convenience.
str
A unique identifier representing your end-user, which can help OpenAI monitor and detect abuse. Learn more.

Response

Returns a CreateEmbeddingResponse object:

Examples

Single Text Embedding

Multiple Texts in One Request

Using Custom Dimensions

Embedding Tokens Directly

Async Usage

Notes

  • The Python SDK automatically optimizes embedding encoding by using base64 format by default and decoding it to floats
  • If NumPy is installed, the SDK uses it for faster base64 decoding
  • See the Embeddings Guide for best practices and use cases
  • Use tiktoken to count tokens before sending requests