> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apigo.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Gemini 스트리밍 채팅 예시

> Gemini `streamGenerateContent`의 스트리밍 채팅 예시.

## 권장 엔드포인트

* [Gemini /v1beta/models/{model}:streamGenerateContent](/ko/api-reference/endpoints/gemini/text-stream-generate-content)

## 최소한의 요청

```json theme={null}
{
  "model": "gemini-2.5-flash",
  "contents": [
    {
      "role": "user",
      "parts": [{ "text": "Explain SSE streaming while streaming the answer." }]
    }
  ]
}
```

## cURL 예

```bash theme={null}
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:streamGenerateContent?alt=sse" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -N \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [{ "text": "Explain SSE streaming while streaming the answer." }]
      }
    ]
  }'
```

## 파이썬 예제

```python theme={null}
from google import genai

client = genai.Client(api_key="<GEMINI_API_KEY>")

stream = client.models.generate_content_stream(
    model="gemini-2.5-flash",
    contents="Explain SSE streaming while streaming the answer.",
)

for chunk in stream:
    if chunk.text:
        print(chunk.text, end="")
```

## Node.js 예

```js theme={null}
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

const stream = await ai.models.generateContentStream({
  model: "gemini-2.5-flash",
  contents: "Explain SSE streaming while streaming the answer."
});

for await (const chunk of stream) {
  if (chunk.text) process.stdout.write(chunk.text);
}
```

## 모범 사례

* 청크당 완전한 문장을 가정하는 대신 `parts[].text`를 점진적으로 누적합니다.
* 구조화된 출력도 스트리밍할 수 있지만 부분 JSON 서버 측을 구문 분석합니다.
* 플래시급 모델은 일반적으로 지연 시간에 민감한 스트리밍 채팅에 가장 적합합니다.
