Xatolarni boshqarish
Muvaffaqiyatsiz so'rov HTTP holat kodi va xato turi hamda xabarli JSON tanasini qaytaradi. Bu sahifa har bir holatni sanab o'tadi, har bir API formatining xato tanasini ko'rsatadi va qaysi xatolarni qayta yuborishga arziydi, shuni aytadi.
SDK'lar holat kodi va tana maydonlarini olib keladigan xato obyektini ko'taradi. Bu so'rov mavjud bo'lmagan modelni nomlaydi:
import openai
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.shannon-ai.com/v1",
)
try:
client.chat.completions.create(
model="no-such-model",
messages=[{"role": "user", "content": "Hello"}],
)
except openai.APIStatusError as error:
print(error.status_code) # 400
print(error.type) # invalid_request_error
print(error.body["message"]) # unknown model: no-such-model
print(error.response.headers.get("x-request-id")) # the id of this request import OpenAI from "openai";
const client = new OpenAI({
apiKey: "YOUR_API_KEY",
baseURL: "https://api.shannon-ai.com/v1",
});
try {
await client.chat.completions.create({
model: "no-such-model",
messages: [{ role: "user", content: "Hello" }],
});
} catch (error) {
if (!(error instanceof OpenAI.APIError)) throw error;
console.log(error.status); // 400
console.log(error.type); // invalid_request_error
console.log(error.error.message); // unknown model: no-such-model
} curl -i https://api.shannon-ai.com/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "no-such-model",
"messages": [{"role": "user", "content": "Hello"}]
}' U 400 holatini va shu tanani qaytaradi:
{
"error": {
"type": "invalid_request_error",
"message": "unknown model: no-such-model"
}
} Xato tanasi
Ikki xil tana bor. /v1/messages Anthropic formatida javob beradi; boshqa har bir endpoint OpenAI formatida. Ikkalasida ham tafsilotlar error.type va error.messageda, shuning uchun bitta o'quvchi barcha endpointlarga yetadi.
{
"error": {
"type": "authentication_error",
"message": "Invalid API key",
"code": "invalid_api_key",
"param": null
}
} {
"type": "error",
"error": {
"type": "authentication_error",
"message": "Invalid API key"
}
} | Maydon | Tur | Tavsif |
|---|---|---|
error.type | string | Xato turi. Quyida Xato turlariga qarang. |
error.message | string | Inson uchun gap. Quyidagi jadvallardagi matnlar o'zgaruvchan qismlaridan tashqari barqaror, ular <id>, <n> yoki … ko'rinishida ko'rsatilgan. |
error.code | string | null | OpenAI formatidagi ba'zi xatolarda bor: 401 bilan invalid_api_key, 413 bilan request_too_large, suhbat juda uzun bo'lganda context_length_exceeded, boshqa hollarda null yoki yo'q. Avval holat va error.type bo'yicha qaror qiling, codeni qo'shimcha deb hisoblang. |
error.param | null | code bilan bir xil xatolarda bor. Doim null. |
type | string | Faqat Anthropic formati. Doim error. |
Xato turlari
| Tur | Holatlar | Tavsif |
|---|---|---|
invalid_request_error | 400, 413, 415, 422 | So'rovning o'zi noto'g'ri: maydon, tana, model id yoki model qabul qilmaydigan kirish. Qayta yuborishdan oldin so'rovni o'zgartiring. |
authentication_error | 401 | API kalit yo'q, noma'lum yoki bekor qilingan. |
not_found_error | 404 | Yo'l mavjud emas. |
rate_limit_error | 429 | Balans so'rovni qoplamaydi, flood himoyasi uni to'xtatdi yoki model band. Qaysi biri ekanini xabar aytadi. |
api_error | 500, 502, 503, 504 | So'rov to'g'ri edi, nosozlik bizning tomonda. Uni qayta yuboring. |
server_error | 500 | So'rov to'g'ri edi, nosozlik bizning tomonda. Uni qayta yuboring. |
Holat kodlari
| Holat | Tur | Xabar | Qachon |
|---|---|---|---|
400 | invalid_request_error | Failed to parse the request body as JSON: … | Tana yaroqli JSON emas. |
400 | invalid_request_error | unknown model: <id> | model e'lon qilingan id emas. /v1/responses da model yo'q bo'lganda ham. |
400 | invalid_request_error | No user message provided | Shannon darajalari: so'rovda foydalanuvchi matni yo'q. |
400 | invalid_request_error | <id> does not accept image input | Rasm kirishi yo'q xostingdagi open-weight modelga rasm yuborilgan. |
400 | invalid_request_error | audio and video input are not supported by any hosted model | Xostingdagi open-weight modelga audio yoki video qismi yuborilgan. |
400 | invalid_request_error | <id> does not accept response_format | Tuzilmali chiqish unga ega bo'lmagan xostingdagi open-weight modeldan so'ralgan. |
400 | invalid_request_error | <id> does not accept response_format json_schema; use json_object | Faqat json_objectni qabul qiladigan xostingdagi open-weight modelga JSON sxemasi yuborilgan. |
400 | invalid_request_error | unknown reasoning effort '<value>'; expected off, low, medium or high | Mulohaza darajasi ro'yxatdagi qiymatlardan biri emas. Xostingdagi open-weight modellar. |
400 | invalid_request_error | count_tokens is available for the hosted open models; unknown model: <id> | Token sanash xostingdagi open-weight modellardan tashqari model uchun so'ralgan. |
401 | authentication_error | Missing authentication | API kalit yuborilmagan. |
401 | authentication_error | Invalid API key | Kalit noma'lum, yangisi bilan almashtirilgan yoki bekor qilingan. |
404 | not_found_error | Not Found | Yo'l mavjud emas, masalan /v1/embeddings. |
405 | api_error | Method Not Allowed | Yo'l mavjud, lekin bu HTTP metodi bilan emas. |
413 | invalid_request_error | Failed to buffer the request body: … | So'rov tanasi 32 MiB dan katta. |
415 | invalid_request_error | Expected request with `Content-Type: application/json` | Content-Type sarlavhasi yo'q yoki application/json emas. |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | Majburiy maydon yo'q, masalan messages, yoki biror maydon noto'g'ri JSON turida. |
429 | rate_limit_error | Quota exceeded. Upgrade your plan at shannon-ai.com/plan | So'rovning chiqish limiti balansingizda qolgan miqdordan katta. |
429 | rate_limit_error | Too many requests. Retry in <n>s. | Flood himoyasi: hisobingizdan bir daqiqada 120 tadan ortiq so'rov. <n> — daqiqa tugashigacha soniyalar soni. |
429 | rate_limit_error | Shannon Coder call quota reached. Upgrade your plan at shannon-ai.com/plan | /v1/chat/completions va /v1/messages da shannon-coder-1: rejangizning joriy 4 soatlik oynadagi Shannon Coder chaqiruvlari ishlatib bo'lingan. |
429 | rate_limit_error | Shannon routes are temporarily busy. Please retry. | Model hozir band. |
500 | server_error | The model backend failed to answer. Please retry. | Model javob yaratmadi. |
500 | server_error | An internal error occurred. Please retry. | So'rov tayyorlanayotganda ichki nosozlik. |
502 | api_error | The model backend failed to answer. Please retry. | Model javob yaratmadi. Shannon 3 oilasi va xostingdagi open-weight modellar. |
503 | api_error | Could not verify your quota right now. Please retry. | Balansingizni hozir o'qib bo'lmadi. |
503 | api_error | token counting is temporarily unavailable for this model | Token sanash bu model uchun hozir mavjud emas. |
504 | api_error | upstream timed out | Xostingdagi open-weight model oqimsiz so'rovning vaqt chegarasidan uzoqroq vaqt oldi. |
Tekshiruvlar shu tartibda bajariladi: API kalit, so'rov tanasi, model id, flood himoyasi, balans. Shuning uchun noto'g'ri kalit va noto'g'ri tanali so'rov 401 qaytaradi. 4xx holati bilan rad etilgan so'rov uchun to'lov olinmaydi.
Balans va flood himoyasi
Har bir so'rovga teng xizmat ko'rsatiladi: tezlik darajalari va alohida API kvotasi yo'q. Shunday bo'lsa-da, ikki narsa 429 bilan javob berishi mumkin, ikkalasi ham rate_limit_error turi bilan. Ularni xabar ajratib beradi.
Balans
Balansingiz — 00:00 UTC da yangilanadigan bugungi reja limiti, undan keyin sotib olingan kredit. Chat va API bir xil balansdan oladi.
Har bir so'rov avval o'zining chiqish limitini ajratib qo'yadi: max_tokens yoki /v1/responses da max_output_tokens, ikkalasini yubormasangiz 4,096. Javob to'liq bo'lgach, ajratilgan miqdor so'rov ishlatgan miqdor bilan almashtiriladi. Agar miqdor qolgan balansga sig'masa, so'rov model boshlanishidan oldin Quota exceededni qaytaradi.
Bir vaqtda ishlayotgan so'rovlarning har biri o'z miqdorini ajratib qo'yadi. Shuning uchun yuqori max_tokensli ko'p parallel so'rov, har bir javob qisqa bo'lsa ham, balansni bir lahzaga tugatishi mumkin. Pastroq max_tokens kamroq ajratadi.
Flood himoyasi
Flood himoyasi hisob uchun daqiqasiga 120 ta so'rovga ruxsat beradi. U kalitni emas, hisobni sanaydi, /v1/chat/completions, /v1/responses va /v1/messages birgalikda. Daqiqa birinchi so'rovingiz bilan boshlanadi.
Shu daqiqadagi 121-so'rov Too many requests. Retry in <n>s.ni qaytaradi va hech narsa turmaydi. Rad etilgan so'rovlar ham hisobga olinadi, shuning uchun kutmasdan qayta yuboradigan mijoz daqiqa tugaguncha rad etilaveradi. Xabardagi <n> soniyani kuting.
Parallel yuborilgan so'rovlar parallel bo'lgani uchun rad etilmaydi. Ular navbatda kutadi va ketma-ket javob beriladi.
GET /v1/models va token sanash endpointlari hisobga olinmaydi.
Limitlar va balans haqida ko'proq, hamda o'z balansingiz va foydalanishingizni qayerda ko'rish mumkin: Limitlar va balans Keys & usage
Nimani qayta urinish kerak
So'rovni faqat xuddi shu so'rov muvaffaqiyatli bo'lishi mumkin bo'lganda qayta yuboring. OpenAI va Anthropic SDK'lari 429 va 5xx javoblarini o'zlari ikki marta qayta urinadi; buni o'zgartirish uchun max_retriesni (JavaScript'da maxRetries) o'rnating.
| Xato | Qayta yuborilsinmi? | Qanday |
|---|---|---|
400, 404, 405, 413, 415, 422 | Yo'q. Avval so'rovni to'g'rilang. | Xabar noto'g'ri maydonni, modelni yoki kirishni nomlaydi. |
401 | Yo'q. Yaroqli kalitdan foydalaning. | Kalitni Keys & usage sahifasida yarating yoki almashtiring. |
429 Too many requests | Ha, kutgandan so'ng. | Xabardagi soniyalar sonicha kuting, so'ng xuddi shu so'rovni yuboring. |
429 Quota exceeded | Hozirgi holatda emas. | max_tokensni kamaytiring, 00:00 UTC dagi kunlik yangilanishni kuting yoki kredit qo'shing. |
429 Shannon Coder call quota reached | Ha, keyingi oynada. | Oynalar har 4 soatda 00:00 UTC dan boshlanadi. Boshqa model darhol javob beradi. |
429 … temporarily busy | Ha. | Bir necha soniya kuting, keyin har bir keyingi urinishda uzoqroq. |
500, 502, 503 | Ha. | 1 soniya, so'ng 2, 4 va 8 soniya kuting. Bir necha urinishdan keyin to'xtang va so'rov id'sini xabar qiling. |
504 | Ha. | Uni streamni true qilib yuboring: vaqt chegarasi oqimsiz javoblarga qo'llanadi. |
Shu jadvalga amal qiladigan qayta urinish sikli:
import re
import time
import openai
from openai import OpenAI
# max_retries=0: this loop does the retrying, the SDK does not add its own.
client = OpenAI(api_key="YOUR_API_KEY", base_url="https://api.shannon-ai.com/v1", max_retries=0)
def create_with_retry(attempts=5, **request):
for attempt in range(attempts):
try:
return client.chat.completions.create(**request)
except openai.RateLimitError as error:
text = str(error)
flood = re.search(r"Retry in (\d+)s", text)
if flood:
time.sleep(int(flood.group(1))) # flood protection: wait out the minute
elif "temporarily busy" in text:
time.sleep(2 ** attempt)
else:
raise # balance: a wait of seconds does not help
except (openai.InternalServerError, openai.APIConnectionError):
time.sleep(2 ** attempt) # 1, 2, 4, 8 seconds
raise RuntimeError("the request failed after all attempts")
response = create_with_retry(
model="shannon-3",
messages=[{"role": "user", "content": "Hello"}],
)
print(response.choices[0].message.content) import OpenAI from "openai";
// maxRetries: 0 — this loop does the retrying, the SDK does not add its own.
const client = new OpenAI({ apiKey: "YOUR_API_KEY", baseURL: "https://api.shannon-ai.com/v1", maxRetries: 0 });
const sleep = (seconds) => new Promise((resolve) => setTimeout(resolve, seconds * 1000));
async function createWithRetry(request, attempts = 5) {
for (let attempt = 0; attempt < attempts; attempt++) {
try {
return await client.chat.completions.create(request);
} catch (error) {
const text = String(error.message);
const flood = text.match(/Retry in (\d+)s/);
if (error.status === 429 && flood) {
await sleep(Number(flood[1])); // flood protection: wait out the minute
} else if (error.status === 429 && text.includes("temporarily busy")) {
await sleep(2 ** attempt);
} else if (error.status >= 500 || error instanceof OpenAI.APIConnectionError) {
await sleep(2 ** attempt); // 1, 2, 4, 8 seconds
} else {
throw error; // wrong request, wrong key or balance
}
}
}
throw new Error("the request failed after all attempts");
}
const response = await createWithRetry({
model: "shannon-3",
messages: [{ role: "user", content: "Hello" }],
});
console.log(response.choices[0].message.content); Oqim ichidagi xatolar
Oqimning holati birinchi baytdan 200. Shundan keyingi nosozlik oqim ichida hodisa sifatida keladi: /v1/chat/completions da error a'zosi bor chunk, /v1/messages da error hodisasi, /v1/responses da response.failed yoki error hodisasi. Oqimning yakunlovchi hodisalari baribir ergashadi. Bunday javobni muvaffaqiyatsiz deb hisoblang va so'rovni 5xx holatidagidek qayta yuboring.
Modelning kontekst oynasidan uzun suhbat shunday bildiriladi, invalid_request_error turi va context_length_exceeded kodi bilan. Qayta yuborishdan oldin suhbatni qisqartiring.
Aniq freymlar streaming bilan birga tasvirlangan. Oqim
So'rov id'lari
Har bir javob, xatolar ham, x-request-id sarlavhasiga ega. U o'z x-request-id sarlavhangizda yuborgan qiymat, yubormagan bo'lsangiz 12 ta o'n oltilik belgi. Uni jurnalga yozing va so'rov bo'yicha yordam so'raganda keltiring.