채팅 컴플리션, Responses API에서의 이미지 생성
채팅 컴플리션, Responses API에서의 이미지 생성 (Image Generation in Chat Completions, Responses API)
이 가이드는 chat/completions 사용 시 이미지를 생성하는 방법을 다뤄요. 참고: Responses API에서 원한다면 여기에서 Feature Request를 제출하세요.
LiteLLM v1.76.1+ 필요
지원 프로바이더:
- Google AI Studio (
gemini) - Vertex AI (
vertex_ai/)
LiteLLM은 채팅 컴플리션 중 이미지 생성을 지원하는 모델의 assistant 메시지에서 images 응답을 표준화합니다.
"message": {
...
"content": "Here's the image you requested:",
"images": [
{
"image_url": {
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...",
"detail": "auto"
},
"index": 0,
"type": "image_url"
}
]
}
빠른 시작
- SDK
- PROXY
from litellm import completion
import os
os.environ["GEMINI_API_KEY"] = "your-api-key"
response = completion(
model="gemini/gemini-2.5-flash-image-preview",
messages=[
{"role": "user", "content": "Generate an image of a banana wearing a costume that says LiteLLM"}
],
)
print(response.choices[0].message.content) # Text response
print(response.choices[0].message.images) # List of image objects
- config.yaml 설정
model_list:
- model_name: gemini-image-gen
litellm_params:
model: gemini/gemini-2.5-flash-image-preview
api_key: os.environ/GEMINI_API_KEY
- proxy server 실행
litellm --config config.yaml
# RUNNING on http://0.0.0.0:4000
- 테스트!
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ***" \
-d '{
"model": "gemini-image-gen",
"messages": [
{
"role": "user",
"content": "Generate an image of a banana wearing a costume that says LiteLLM"
}
]
}'
기대 응답:
{
"id": "chatcmpl-3b66124d79a708e10c603496b363574c",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "Here's the image you requested:",
"role": "assistant",
"images": [
{
"image_url": {
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...",
"detail": "auto"
},
"index": 0,
"type": "image_url"
}
]
}
}
],
"created": 1723323084,
"model": "gemini/gemini-2.5-flash-image-preview",
"object": "chat.completion",
"usage": {
"completion_tokens": 12,
"prompt_tokens": 16,
"total_tokens": 28
}
}
출처: 문서
본문
스트리밍 지원
- SDK
- PROXY
from litellm import completion
import os
os.environ["GEMINI_API_KEY"] = "your-api-key"
response = completion(
model="gemini/gemini-2.5-flash-image-preview",
messages=[
{"role": "user", "content": "Generate an image of a banana wearing a costume that says LiteLLM"}
],
stream=True,
)
for chunk in response:
if hasattr(chunk.choices[0].delta, "images") and chunk.choices[0].delta.images is not None:
print("Generated image:", chunk.choices[0].delta.images[0]["image_url"]["url"])
break
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ***" \
-d '{
"model": "gemini-image-gen",
"messages": [
{
"role": "user",
"content": "Generate an image of a banana wearing a costume that says LiteLLM"
}
],
"stream": true
}'
기대 스트리밍 응답:
data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1723323084,"model":"gemini/gemini-2.5-flash-image-preview","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}
data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1723323084,"model":"gemini/gemini-2.5-flash-image-preview","choices":[{"index":0,"delta":{"content":"Here's the image you requested:"},"finish_reason":null}]}
data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1723323084,"model":"gemini/gemini-2.5-flash-image-preview","choices":[{"index":0,"delta":{"images":[{"image_url":{"url":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...","detail":"auto"},"index":0,"type":"image_url"}]},"finish_reason":null}]}
data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1723323084,"model":"gemini/gemini-2.5-flash-image-preview","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]
Async 지원
from litellm import acompletion
import asyncio
import os
os.environ["GEMINI_API_KEY"] = "your-api-key"
async def generate_image():
response = await acompletion(
model="gemini/gemini-2.5-flash-image-preview",
messages=[
{"role": "user", "content": "Generate an image of a banana wearing a costume that says LiteLLM"}
],
)
print(response.choices[0].message.content) # Text response
print(response.choices[0].message.images) # List of image objects
return response
# Run the async function
asyncio.run(generate_image())
지원 모델
| 프로바이더 | 모델 |
|---|---|
| Google AI Studio | gemini/gemini-2.0-flash-preview-image-generation, gemini/gemini-2.5-flash-image-preview, gemini/gemini-3-pro-image-preview |
| Vertex AI | vertex_ai/gemini-2.0-flash-preview-image-generation, vertex_ai/gemini-2.5-flash-image-preview, vertex_ai/gemini-3-pro-image-preview |
스펙
응답의 images 필드는 다음 구조를 따릅니다:
"images": [
{
"image_url": {
"url": "data:image/png;base64,<base64_encoded_image>",
"detail": "auto"
},
"index": 0,
"type": "image_url"
}
]
images-List[ImageURLListItem]: 생성된 이미지 배열image_url-ImageURLObject: 이미지 데이터 컨테이너url-str: data URI 형식의 base64 인코딩 이미지 데이터detail-str: 이미지 상세 수준 (생성된 이미지에서는 항상"auto")index-int: 응답에서 이미지의 인덱스type-str: 타입 식별자 (항상"image_url")
이미지는 base64로 인코딩된 data URI로 반환되며, HTML <img> 태그에 직접 사용하거나 파일로 저장할 수 있어요.