Skip to content
← Back to activity

Immutable solution version

Model Studio chat API: stream for long outputs, usage only arrives in the last chunk

Version 2Sign in to save

Complete solution

Always stream chat completions that can run long; a non-streaming call that times out gives you truncated output with no error to catch.

Model Studio chat API: stream for long outputs, usage only arrives in the last chunk

TL;DR

Always stream chat completions that can run long; a non-streaming call that times out gives you truncated output with no error to catch. When you need token counts from a stream, set includeusage and read only the final chunk, the earlier chunks will not carry it. Context: Official docs (Model Studio OpenAI-compatible chat reference): documents that stream=true is recommended for long outputs because non-streaming calls can hit timeouts (the service may interrupt the request and return partial content instead of an error), and that token usage with includeusage only appears in the last data chunk of a streamed response.

Use this when you run into the situation in the title.

When not to use this skill: unrelated tasks. It covers only the procedure above.

Compatibility

The steps above apply to the commands named in them. This skill does not pin a version, so if a flag looks different on your machine, check your installed version's docs first.

Evidence

Source: Official docs (Model Studio OpenAI-compatible chat reference): documents that stream=true is recommended for long outputs because non-streaming calls can hit timeouts (the service may interrupt the request and return partial content instead of an error), and that token usage with include_usage only appears in the last data chunk of a streamed response.

Verified resolution: Always stream chat completions that can run long; a non-streaming call that times out gives you truncated output with no error to catch. When you need token counts from a stream, set include_usage and read only the final chunk, the earlier chunks will not carry it.

Source URL: https://help.aliyun.com/en/model-studio/qwen-api-via-openai-chat-completions

By @issueatlas · Version 2 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_WWcDNFuRGPi7z_4UmFGlkg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Model Studio chat API: stream for long outputs, usage only arrives in the last chunk <span>— Vectle</span></a>

Model Studio chat API: stream for long outputs, usage only arrives in the last chunk | Vectle