Extract text, structured data, and key information from images using the Qwen-OCR model. Qwen-OCR supports two API protocols: the OpenAI-compatible API and the DashScope API.
Related topic: Text extraction (Qwen-OCR)
OpenAI-compatible API
- China (Beijing) region
- Singapore region
- US (Virginia) region
base_url for SDK calls: https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1The endpoint for HTTP calls: POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completionsYou must first obtain and configure an API key and set the API key as an environment variable. If you call the API through the OpenAI SDK, install the SDK.
Request bodymodelstring (required)The model name. For supported models, see Select a model.messagesarray (required)The context passed to the model, arranged in conversation order.
Message types User Message object (required)The user message, used to pass instructions and the images to recognize to the model.
Attributes content array (required)The message content.
Attributes type string (required)Valid values:
string (optional)The input text.Default value: Please output only the text content from the image without any additional descriptions or formatting., that is, by default the model extracts all text from the image.image_urlobjectInformation about the input image. Required when type is image_url.
Attributes url string (required)The image URL or Base64 Data URL. To pass a local file, see Text extraction.integer (optional)Sets the minimum pixel threshold for the input image, in pixels.When the input image has fewer pixels than min_pixels, the image is enlarged until the total number of pixels exceeds min_pixels.
Conversion between image tokens and pixels The number of pixels corresponding to each image token varies by model:
min_pixels value range
{"type": "image_url","image_url": {"url":"https://xxxx.jpg"},"min_pixels": 3072}max_pixelsinteger (optional)Sets the maximum pixel threshold for the input image, in pixels.When the input image is within the [min_pixels, max_pixels] range, the model recognizes the original image. When the input image has more pixels than max_pixels, the image is reduced until the total number of pixels falls below max_pixels.
Conversion between image tokens and pixels The number of pixels corresponding to each image token varies by model:
max_pixels value range
{"type": "image_url","image_url": {"url":"https://xxxx.jpg"},"max_pixels": 8388608}string (required)The role of the user message, fixed to user.boolean (optional) Default value: falseWhether to return responses in streaming mode.Valid values:
object (optional)Configuration items for streaming output, effective only when stream is true.
Attributes include_usage boolean (optional) Default value: falseWhether to include token usage information in the last data chunk.Valid values:
integer (optional)Limits the maximum number of tokens the model generates. If the generated content exceeds this value, the response is truncated.
boolean (optional) Default value: falseWhether to return the log probabilities of output tokens. Valid values:
integer (optional) Default value: 0Specifies the number of the most likely candidate tokens to return at each generation step.Value range: [0,5]Effective only when logprobs is true.temperaturefloat (optional) Default value: 0.01The sampling temperature, which controls the diversity of the text generated by the model.A higher temperature produces more diverse text, while a lower temperature produces more deterministic text.Value range: [0, 2)Both temperature and top_p control the diversity of generated text. We recommend setting only one of them.We recommend keeping the default value.top_p float (optional) Default value: 0.001The probability threshold for nucleus sampling, which controls the diversity of the text generated by the model.A higher top_p produces more diverse text, while a lower top_p produces more deterministic text.Value range: (0,1.0]Both temperature and top_p control the diversity of generated text. We recommend setting only one of them.We recommend keeping the default value.top_k integer (optional) Default value: 1The size of the candidate set sampled during generation. For example, when set to 50, only the 50 highest-scoring tokens from a single generation form the random sampling candidate set. A larger value increases randomness, while a smaller value increases determinism. When set to None or greater than 100, the top_k policy is not enabled and only the top_p policy takes effect.The value must be greater than or equal to 0.This is not an OpenAI standard parameter. When calling through the Python SDK, put it in the extra_body object, configured as: extra_body={"top_k": xxx}; when calling through the Node.js SDK or HTTP, pass it as a top-level parameter.We recommend keeping the default value.repetition_penalty float (optional) Default value: 1.0The repetition level in consecutive sequences during model generation. Increasing repetition_penalty reduces the repetition in model output, and 1.0 means no penalty. This parameter has a significant impact on model performance, so we recommend keeping the default value.We recommend keeping the default value.presence_penalty float (optional) Default value: 0.0Controls the content repetition level when the model generates text.Value range: [-2.0, 2.0]. Positive values reduce repetition, and negative values increase it.We recommend increasing this value in scenarios that require diversity, fun, or creativity such as creative writing or brainstorming; we recommend decreasing it in scenarios that emphasize consistency and terminology accuracy such as technical documents or formal text.
How it works If the parameter value is positive, the model applies a penalty to tokens already present in the current text (the penalty is independent of how many times the text appears), reducing the chance of these tokens appearing again, thereby reducing content repetition and increasing word diversity. We recommend keeping the default value.seed integer (optional)The random number seed, used to ensure that results are reproducible with the same input and parameters. If the same seed is passed with other parameters unchanged, the model will return the same result as much as possible.Value range: [0,2 31 −1].We recommend keeping the default value.stop string or array (optional)Specifies stop words. When a string or token_id specified by stop appears in the text generated by the model, generation stops immediately.You can pass sensitive words to control the model output.When stop is an array, you cannot mix |
Python |
chat response object (non-streaming output)idstringThe unique identifier of this request.choicesarrayThe array of content generated by the model.
Attributes finish_reason stringThe reason the model stopped generating.There are two cases:
integerThe index of the current object in the choices array.messageobjectThe message output by the model.
Attributes content stringThe result returned by the model.processed_text stringThe post-processed result of the model's raw output, which automatically removes repeated segments. When the model output contains repeated content, this field provides the cleaned text.Returned only when called through the DashScope SDK and curl. The OpenAI-compatible SDK does not return this field.refusal stringThis parameter is currently fixed to null.role stringThe role of the message, fixed to assistant.audio objectThis parameter is currently fixed to null.function_call objectThis parameter is currently fixed to null.tool_calls arrayThis parameter is currently fixed to null.integerThe timestamp when this request was created.modelstringThe model used for this request.object stringAlways chat.completion.service_tier stringThis parameter is currently fixed to null.system_fingerprintstringThis parameter is currently fixed to null.usage objectThe token usage information of this request.
Attributes completion_tokens integerThe number of tokens generated by the model.prompt_tokens integerThe number of input tokens.total_tokens integerThe total number of tokens consumed, the sum of prompt_tokens and completion_tokens.completion_tokens_details objectThe fine-grained classification of output tokens.
Attributes accepted_prediction_tokens integerThis parameter is currently fixed to null.audio_tokens integerThis parameter is currently fixed to null.reasoning_tokens integerThis parameter is currently fixed to null.text_tokens integerThe number of tokens corresponding to the model's text output.rejected_prediction_tokensintegerThis parameter is currently fixed to null.objectThe fine-grained classification of input tokens.
Attributes audio_tokens integerThis parameter is currently fixed to null.cached_tokens integerThis parameter is currently fixed to null.text_tokens integerThe number of tokens corresponding to the model's text input.image_tokens integerThe number of tokens corresponding to the model's image input. |
chat response chunk object (streaming output)idstringThe unique identifier of this call. Each chunk object has the same id.choicesarrayThe array of content generated by the model. Empty in the last chunk if include_usage is set to true.
Attributes delta objectThe output content returned in streaming mode.
Attributes content stringThe result returned by the model.function_call objectThis parameter is currently fixed to null.refusal objectThis parameter is currently fixed to null.role stringThe role of the message object, which only has a value in the first chunk.stringThe reason the model stopped generating. There are three cases:
integerThe index of the current response in the choices array.integerThe timestamp when this request was created. Each chunk has the same timestamp.modelstringThe model used for this request.object stringAlways chat.completion.chunk.service_tier stringThis parameter is currently fixed to null.system_fingerprintstringThis parameter is currently fixed to null.usage objectThe tokens consumed by this request. Returned only in the last chunk when include_usage is true.
Attributes completion_tokens integerThe number of tokens generated by the model.prompt_tokens integerThe number of input tokens.total_tokens integerThe total number of tokens consumed, the sum of prompt_tokens and completion_tokens.completion_tokens_details objectThe fine-grained classification of output tokens.
Attributes accepted_prediction_tokens integerThis parameter is currently fixed to null.audio_tokens integerThis parameter is currently fixed to null.reasoning_tokens integerThis parameter is currently fixed to null.text_tokens integerThe number of tokens corresponding to the model's text output.rejected_prediction_tokensintegerThis parameter is currently fixed to null.objectThe fine-grained classification of input tokens.
Attributes audio_tokens integerThis parameter is currently fixed to null.cached_tokens integerThis parameter is currently fixed to null.text_tokens integerThe number of tokens corresponding to the model's text input.image_tokens integerThe number of tokens corresponding to the model's image input. |
DashScope
- China (Beijing) region
- Singapore region
- US (Virginia) region
endpoint for HTTP calls: POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generationNo region configuration is required for SDK calls (defaults to cn-beijing).You must first obtain and configure an API key and set the API key as an environment variable. If you call the API through the DashScope SDK, install the DashScope SDK.
Request bodymodelstring (required)The model name. For supported models, see Select a model.messagesarray (required)The context passed to the model, arranged in conversation order.When calling through HTTP, put messages in the input object.
Message types User Message object (required)The user message, used to pass questions, instructions, or context to the model.
Attributes content string or array (required)The message content. If the input is text only, it is of string type; if the input contains image data, it is of array type.
Attributes text string (optional)The input text.Default value: Please output only the text content from the image without any additional descriptions or formatting., that is, by default the model extracts all text from the image.imagestring (optional)The image URL, Base64 Data URL, or local path. To pass a local file, see Passing local files.Example value: {"image":"https://xxxx.jpeg"}enable_rotateboolean (optional) Default value: falseWhether to correct skewed images.Valid values:
{"image":"https://xxxx.jpeg","enable_rotate": True}min_pixelsinteger (optional)Sets the minimum pixel threshold for the input image, in pixels.When the input image has fewer pixels than min_pixels, the image is enlarged until the total number of pixels exceeds min_pixels.
Conversion between image tokens and pixels The number of pixels corresponding to each image token varies by model:
min_pixels value range
{"image":"https://xxxx.jpeg","min_pixels": 3072}max_pixelsinteger (optional)Sets the maximum pixel threshold for the input image, in pixels.When the input image is within the [min_pixels, max_pixels] range, the model recognizes the original image. When the input image has more pixels than max_pixels, the image is reduced until the total number of pixels falls below max_pixels.
Conversion between image tokens and pixels The number of pixels corresponding to each image token varies by model:
max_pixels value range
{"image":"https://xxxx.jpeg","max_pixels": 8388608}string (required)The role of the user message, fixed to user.integer (optional)Limits the maximum number of tokens the model generates. If the generated content exceeds this value, the response is truncated.
In the Java SDK, this is maxTokens. When calling through HTTP, put max_tokens in the parameters object.ocr_optionsobject (optional)Parameters required when using the Qwen OCR model to call a built-in task. When calling a built-in task, you do not need to pass a User Message; the model uses the prompt of the corresponding task internally. For more information, see Calling built-in tasks.
Attributes task string (required)The name of the built-in task. Valid values:
object (optional)When task is set to key_information_extraction, this parameter specifies the specific fields to extract. If task_config is not specified, the model extracts all fields from the image by default.
Attributes result_schema object (optional)The fields for the model to extract. It must be a JSON object structure with up to three levels of nested JSON objects.Specify the name of the field to extract in the key of the JSON object. The corresponding value can be empty. We recommend providing a field description or format requirements in the value to improve extraction accuracy.Example value:In the Java SDK, this is OcrOptions. The minimum DashScope Python SDK version is 2.0.1, and the minimum Java SDK version is 2.18.4. When calling through HTTP, put ocr_options in the parameters object.seed integer (optional)The random number seed, used to ensure that results are reproducible with the same input and parameters. If the same seed is passed with other parameters unchanged, the model will return the same result as much as possible.Value range: [0,2 31 −1].We recommend keeping the default value. When calling through HTTP, put seed in the parameters object.temperature float (optional) Default value: 0.01The sampling temperature, which controls the diversity of the text generated by the model.A higher temperature produces more diverse text, while a lower temperature produces more deterministic text.Value range: [0, 2)Both temperature and top_p control the diversity of generated text. We recommend setting only one of them.We recommend keeping the default value. When calling through HTTP, put temperature in the parameters object.top_p float (optional) Default value: 0.001The probability threshold for nucleus sampling, which controls the diversity of the text generated by the model.A higher top_p produces more diverse text, while a lower top_p produces more deterministic text.Value range: (0,1.0]Both temperature and top_p control the diversity of generated text. We recommend setting only one of them.We recommend keeping the default value. In the Java SDK, this is topP. When calling through HTTP, put top_p in the parameters object.top_k integer (optional) Default value: 1The size of the candidate set sampled during generation. For example, when set to 50, only the 50 highest-scoring tokens from a single generation form the random sampling candidate set. A larger value increases randomness, while a smaller value increases determinism. When set to None or greater than 100, the top_k policy is not enabled and only the top_p policy takes effect.The value must be greater than or equal to 0.This is not an OpenAI standard parameter. When calling through the Python SDK, put it in the extra_body object, configured as: extra_body={"top_k": xxx}; when calling through the Node.js SDK or HTTP, pass it as a top-level parameter.We recommend keeping the default value.repetition_penalty float (optional) Default value: 1.0The repetition level in consecutive sequences during model generation. Increasing repetition_penalty reduces the repetition in model output, and 1.0 means no penalty. This parameter has a significant impact on model performance, so we recommend keeping the default value.We recommend keeping the default value. In the Java SDK, this is repetitionPenalty. When calling through HTTP, put repetition_penalty in the parameters object.presence_penalty float (optional) Default value: 0.0Controls the content repetition level when the model generates text.Value range: [-2.0, 2.0]. Positive values reduce repetition, and negative values increase it.We recommend increasing this value in scenarios that require diversity, fun, or creativity such as creative writing or brainstorming; we recommend decreasing it in scenarios that emphasize consistency and terminology accuracy such as technical documents or formal text.
How it works If the parameter value is positive, the model applies a penalty to tokens already present in the current text (the penalty is independent of how many times the text appears), reducing the chance of these tokens appearing again, thereby reducing content repetition and increasing word diversity. We recommend keeping the default value.stream boolean (optional) Default value: falseWhether to return responses in streaming mode. Parameter values:
This parameter is supported only by the Python SDK. For streaming output through the Java SDK, use theincremental_output boolean (optional) Default value: falseWhether to enable incremental output in streaming mode. We recommend setting it to true first.Parameter values:
In the Java SDK, this is incrementalOutput. When calling through HTTP, put incremental_output in the parameters object.stop string or array (optional)Specifies stop words. When a string or token_id specified by stop appears in the text generated by the model, generation stops immediately.You can pass sensitive words to control the model output.When stop is an array, you cannot mixlogprobs boolean (optional) Default value: falseWhether to return the log probabilities of output tokens. Valid values:
When calling through HTTP, put logprobs in the parameters object.top_logprobs integer (optional) Default value: 0Specifies the number of the most likely candidate tokens to return at each generation step. Effective only when logprobs is true.Value range: [0,5]In the Java SDK, this is topLogprobs. When calling through HTTP, put top_logprobs in the parameters object. |
The following is a code example for calling the high-precision recognition built-in task. For more information, see Calling built-in tasks. python |
chat response object (identical format for streaming and non-streaming output)The following is the raw response returned by the HTTP interface. If the call fails, the Python SDK raises arequest_id stringThe unique identifier of this call.In the Java SDK, the return parameter is requestId.output objectThe call result information.
Attributes text stringThis parameter is currently fixed to null.finish_reasonstringThe reason the model stopped generating. There are the following cases:
arrayThe output information of the model.
Attributes finish_reason stringThere are the following cases:
objectThe message object output by the model.
Attributes role stringThe role of the output message, fixed to assistant.contentobjectThe content of the output message.
Attributes ocr_result objectThe task result information output when the Qwen-OCR series models call the built-in key information extraction or high-precision recognition tasks.
Attributes kv_result arrayThe output result of the key information extraction task.words_infoarrayThe output result of the high-precision recognition task.
Attributes rotate_rect arrayExample value: [center_x, center_y, width, height, angle]The rotated rectangle representation of the text box:
arrayExample value: [x1, y1, x2, y2, x3, y3, x4, y4]The coordinates of the four vertices of the text box, starting from the top-left corner and arranged clockwise in the order top-left → top-right → bottom-right → bottom-left.textstringThe content of the text line.stringThe content of the output message.stringThe post-processed result of the model's raw output, which automatically removes repeated segments. When the model output contains repeated content, this field provides the cleaned text.objectThe probability information of the current choices object.
Attributes content arrayThe token array with log probability information.
Attributes token stringThe current token.bytes arrayThe list of raw UTF-8 bytes of the current token, used to precisely restore the output content, which is helpful for processing emoji and Chinese characters.logprob floatThe log probability of the current token. A null return value indicates an extremely low probability.top_logprobs arrayThe several most likely tokens at the current token position and their log probabilities. The number of elements is consistent with the input parameter top_logprobs.
Attributes token stringThe current token.bytes arrayThe list of raw UTF-8 bytes of the current token, used to precisely restore the output content, which is helpful for processing emoji and Chinese characters.logprob floatThe log probability of the current token. A null return value indicates an extremely low probability.objectThe token usage information of this request.
Attributes input_tokens integerThe number of input tokens.output_tokens integerThe number of output tokens.characters integerThis parameter is currently fixed to 0.input_tokens_detailsobjectThe fine-grained classification of input tokens.
Attributes image_tokens integerThe number of tokens corresponding to the model's image input.text_tokens integerThe number of tokens corresponding to the model's text input.objectThe fine-grained classification of output tokens.
Attributes text_tokens integerThe number of tokens corresponding to the model's text input.integerThe total number of tokens consumed, the sum of input_tokens and output_tokens.image_tokens integerThis field is returned when the input contains image. It is the length of the user's input image content after being converted into tokens. |