AGTCloud API Docs
GuidesAPI ReferenceIntegrations and resourcesUpdate and help
AudioGemini TTS

Streaming speech synthesis (Generate Content)

Synthesize speech with streaming Generate Content and receive audio chunks and final usage.

Synthesize speech from text and receive Base64 audio chunks over SSE. Request fields, voices and two-speaker settings match non-streaming Generate Content; see Gemini TTS models for model selection.

POST/v1beta/models/{model}:streamGenerateContent

Synthesize speech from text and receive Base64 audio chunks over SSE.

Authorization

bearerAuth
AuthorizationBearer <token>

Use Bearer your-api-key in the Authorization Header.

In: header

Path Parameters

model*string

Enter an enabled model ID. See the Gemini TTS model guide for available models.

Query Parameters

alt*"sse"

Use sse to receive the server event stream.

Default"sse"

Value in

  • "sse"

Request Body

application/json

TypeScript Definitions

Use the request body type in TypeScript.

Response Body

text/event-stream

application/json

application/json

SSE data structure

These fields describe the JSON in one data: event, not the complete SSE text stream.

Streaming requests

ItemHandling
RequestUse :streamGenerateContent?alt=sse; keep contents and generationConfig in the body without adding a stream field
Data formatReturns text/event-stream. Split events at blank lines and parse each data: value as JSON
Audio locationRead candidates[].content.parts[].inlineData; group chunks for the same candidate by candidates[].index
Completion and usageCheck finishReason; STOP indicates normal completion. usageMetadata is a cumulative snapshot: keep the latest value without adding snapshots together
End markersDetermine completion from finishReason and the end of the stream; do not wait for Interactions’ interaction.completed or a fixed [DONE] marker

Audio assembly and playback

ItemHandling
Default audioPCM16 little-endian, 24 kHz, mono, without a WAV header. Use the format, sample rate and channels in the actual mimeType
Base64 chunksDecode each inlineData.data separately, then concatenate the bytes in order; do not concatenate Base64 strings directly
Saving and playbackWrap the concatenated raw PCM in WAV, or use a player that accepts raw PCM; do not treat each chunk as a complete WAV file
Empty chunksA chunk may contain only usage or empty text. Continue checking finish reasons and usage; do not treat empty text as audio

Try It provides audio playback and download after the response completes, with SSE on the right. Copying the response preserves all chunks and the original audio data.

Error handling

SituationHandling
Failure before generationUse the HTTP status and JSON error message to check parameters, authentication or access permissions
Failure after streaming startsHTTP 200 only indicates that the response has started. Treat a subsequent error as failure, and do not treat partial audio as a complete result
Connection interruptedWithout a normal finish reason, synthesis is not successful. Retain the request ID and received response, investigate, then retry

Official references