본문으로 건너뛰기

에러 처리

API 요청이 실패하면 HTTP 상태 코드와 함께 JSON 형식의 에러 응답이 반환됩니다.

에러 응답 형식

{
"error": {
"code": "invalid_api_key",
"message": "Invalid API key provided.",
"type": "authentication_error",
"status": 401
}
}

HTTP 상태 코드

코드의미대처 방법
400잘못된 요청요청 파라미터 확인. 필수 필드 누락 또는 타입 오류
401인증 실패API Key 확인. 콘솔에서 재발급
403권한 없음모델 접근 권한 또는 플랜 확인
404리소스 없음엔드포인트 URL 또는 모델 ID 확인
429요청 한도 초과잠시 후 재시도. 지수 백오프 전략 권장
500서버 오류iwinv 서버 오류. 지속 시 기술지원 신청
503서비스 일시 불가모델 서버 과부하. 잠시 후 재시도

재시도 전략 (지수 백오프)

import openai
import time

def chat_with_retry(client, model, messages, max_retries=3):
for attempt in range(max_retries):
try:
return client.chat.completions.create(
model=model,
messages=messages
)
except openai.AuthenticationError:
# 401 — Key 오류, 재시도해도 소용없음
raise
except openai.RateLimitError:
# 429 — 잠시 후 재시도
wait = 2 ** attempt
print(f"Rate limit. {wait}초 후 재시도...")
time.sleep(wait)
except openai.APIStatusError:
# 500, 503 — 서버 오류
if attempt == max_retries - 1:
raise
time.sleep(2 ** attempt)
raise Exception("최대 재시도 횟수 초과")

# 사용 예시
response = chat_with_retry(
client,
model="gemma4-26b",
messages=[{"role": "user", "content": "안녕!"}]
)
경고

Key 인증 오류(401)는 재시도해도 해결되지 않습니다.
API Key가 유효한지, 만료되지 않았는지 먼저 확인하세요.

위험

Key가 정상 작동하지 않는 경우 iwinv 콘솔에서 Key 상태를 확인하거나,
온라인 기술지원을 신청하시기 바랍니다.