Skip to main content
Text Embeddings

OpenAI compatible embedding

OpenAI-compatible embedding

POST
/compatible-mode/v1/embeddings
import os
from openai import OpenAI

client = OpenAI(
  api_key=os.getenv("DASHSCOPE_API_KEY"),  # If you have not configured an environment variable, replace the placeholder with your API key.
  base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1"  
)

completion = client.embeddings.create(
  model="text-embedding-v4",
  input='The clothes are of good quality and look good, definitely worth the wait. I love them.',
  dimensions=1024,
  encoding_format="float"
)

print(completion.model_dump_json())
{
  "data": [
    {
      "embedding": [
        -0.0695386752486229,
        0.030681096017360687
      ],
      "index": 0,
      "object": "embedding"
    },
    {
      "embedding": [
        -0.06348952651023865,
        0.060446035116910934
      ],
      "index": 5,
      "object": "embedding"
    }
  ],
  "model": "text-embedding-v4",
  "object": "list",
  "usage": {
    "prompt_tokens": 184,
    "total_tokens": 184
  },
  "id": "73591b79-d194-9bca-8bb5-xxxxxxxxxxxx"
}
Before you call the API, get an API key and set it as an environment variable. If you use the OpenAI SDK, install it first.

Supported models

ModelDimensionsMax tokensBatch sizeLanguagesPrice (per 1M tokens)Free quota
text-embedding-v42048, 1536, 1024 (default), 768, 512, 256, 128, 648,19210100+ major languages$0.071M tokens (90 days)
text-embedding-v31024 (default), 768, 5128,1921050+ languages$0.071M tokens (90 days)

Endpoints

International API keys can only use international endpoints. China API keys can only use China endpoints.
API key fromEndpoint
Internationalhttps://dashscope-intl.aliyuncs.com/compatible-mode/v1/embeddings
Chinahttps://dashscope.aliyuncs.com/compatible-mode/v1/embeddings
SDKs use the base URL without /embeddings.

Authorizations

string
header
required

DashScope API key. Obtain from the Qwen Cloud console.

Body

application/json
string
required

The model to call. Supported values: text-embedding-v4, text-embedding-v3.

text-embedding-v4
string
required

The input text to process. The value can be a string, an array of strings, or a file. When the input is a string, it can contain up to 8,192 tokens. When the input is a list of strings or a file, it can contain up to 10 items (lines), each with a maximum of 8,192 tokens.

enum<integer>
default1024

The embedding dimensions. Valid values: 2048 (text-embedding-v4 only), 1536 (text-embedding-v4 only), 1024, 768, 512, 256 (text-embedding-v4 only), 128 (text-embedding-v4 only), or 64 (text-embedding-v4 only). Default: 1024.

2048,1536,1024,768,512,256,128,64
enum<string>
default"float"

The format of the returned embedding. Only float is supported.

float

Response

200-application/json
object[]

The output data for the task.

string
default"text-embedding-v4"

The model that was called.

string
default"list"

The type of data returned by the call. Default: list.

object
default"{\"prompt_tokens\":184,\"total_tokens\":184}"

Token usage statistics.

string
default"73591b79-d194-9bca-8bb5-xxxxxxxxxxxx"

The unique request ID. Use this ID to trace and troubleshoot requests.