Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

AI & ML · Function

createEmbedding

Creates numerical vector representations of text through the OpenAI embeddings API. These vectors can be used for semantic search, retrieval, and related machine-learning workflows.

How it works

Send text input and an embedding model name to the embeddings API. The response contains embedding vectors in its data collection, with one result corresponding to each supplied input item.

Syntax

client.embeddings.create({ model, input })

Parameters

NameTypeDescription
inputstring | string[]Text string or array of text strings to convert into embedding vectors.
modelstringName of the embedding model, such as text-embedding-3-small.

Returns

A response whose data items contain embedding vectors; one item is returned for each input text.

Key points

  • Call the embeddings endpoint with the text you want represented and the chosen embedding model.
  • The documentation uses text-embedding-3-small as a current example model for generating vectors.
  • For a single input, retrieve the generated vector from the first response item's embedding field.

Common mistakes

  • Normalize embedded text by replacing newline characters when preparing content, as shown in the documentation example.

Example: Create embeddings for multiple texts

import OpenAI from "openai";

const client = new OpenAI();

const response = await client.embeddings.create({
  model: "text-embedding-3-small",
  input: ["A document about machine learning.", "A document about databases."]
});

console.log(response.data.map(item => item.embedding.length));

Creates one embedding vector for each supplied text using the OpenAI embeddings API.

When to use it

Semantic search

Convert documents into vectors that can be indexed and compared for meaning.

Retrieval

Represent a user question and compare it with stored document vectors.

Text feature generation

Transform text into numerical features for downstream machine-learning processing.

Interview questions

QWhat does the embeddings API create?

The method accepts text input and a model identifier, then returns embedding vectors in the response data. For multiple inputs, provide an array and read the corresponding vector from each data item.

Follow-up: How would you embed a batch? Pass an array of strings as input and associate each returned data item with the input at the same position.

QHow do you create an embedding for text?

Use an embedding model such as text-embedding-3-small and provide the text to represent. The returned vector is available from the embedding field of each response item.

Follow-up: What should change when embedding several texts? Supply multiple strings in the input array and process every returned vector rather than only the first one.

QWhere is the generated vector located in the response?

The API response exposes results through data, and each result contains an embedding vector. The vector can then be used by an application for semantic comparison or retrieval workflows.

Follow-up: How can the output support retrieval? Store vectors and compare a query vector with stored vectors using a similarity method implemented by the retrieval system.

  • embeddings API
  • text-embedding-3-small
  • retrieval
  • vector embeddings

Source: Vector embeddings | OpenAI API. Checked 2026-10-15.