MuseSpark
MuseSpark
Guides

Gemini streaming

Request SSE explicitly and preserve native candidate structure.

Call :streamGenerateContent with alt=sse. The query option selects SSE transport; authentication remains in a MuseSpark credential header.

curl --fail-with-body -N \
  'https://api.musespark.ai/proxy/gemini/v1beta/models/YOUR_AUTHORIZED_MODEL_ID:streamGenerateContent?alt=sse' \
  -H "Authorization: Bearer $MUSESPARK_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"contents":[{"role":"user","parts":[{"text":"Describe a paper city."}]}]}'

Parse events incrementally

Use a real SSE parser: an event can span network chunks, and a chunk can contain several events. Each event carries a native Gemini response fragment. Inspect its candidate, content, part, finish, and error information rather than assuming every event contains one text token.

Handle completion and failure

Track the native protocol's completion information separately from transport EOF. An HTTP 200 can be followed by an execution error.

Preserve partial results and request identity. A disconnected paid stream can already have incurred usage; do not treat a retry as free or guaranteed to reuse the original execution.