အမှားကိုင်တွယ်မှု
မအောင်မြင်သော request သည် HTTP status code တစ်ခုနှင့် error type နှင့် မက်ဆေ့ချ်ပါသော JSON body တစ်ခုကို ပြန်ပေးသည်။ ဤစာမျက်နှာတွင် status တိုင်းကို စာရင်းပြုစုပြီး API format တစ်ခုစီ၏ error body ကို ပြကာ မည်သည့်အမှားများကို ပြန်ပို့သင့်သည်ကို ဖော်ပြထားသည်။
SDK များသည် status code နှင့် body ၏ field များကို သယ်ဆောင်သော error object တစ်ခုကို ပေးသည်။ ဤ request သည် မရှိသော model တစ်ခုကို အမည်ပေးထားသည်-
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"}]
}' ၎င်းသည် status 400 နှင့် ဤ body ကို ပြန်ပေးသည်-
{
"error": {
"type": "invalid_request_error",
"message": "unknown model: no-such-model"
}
} Error body
body နှစ်မျိုး ရှိသည်။ /v1/messages သည် Anthropic format ဖြင့် ဖြေပြီး အခြား endpoint အားလုံးသည် OpenAI format ဖြင့် ဖြေသည်။ နှစ်မျိုးလုံးတွင် အသေးစိတ်သည် error.type နှင့် error.message တွင် ရှိသဖြင့် reader တစ်ခုတည်းဖြင့် endpoint အားလုံးကို ဖတ်နိုင်သည်။
{
"error": {
"type": "authentication_error",
"message": "Invalid API key",
"code": "invalid_api_key",
"param": null
}
} {
"type": "error",
"error": {
"type": "authentication_error",
"message": "Invalid API key"
}
} | Field | Type | ဖော်ပြချက် |
|---|---|---|
error.type | string | အမှား၏ အမျိုးအစား။ အောက်ရှိ Error types ကို ကြည့်ပါ။ |
error.message | string | လူတစ်ယောက်အတွက် စာကြောင်းတစ်ကြောင်း။ အောက်ပါဇယားများရှိ စာသားများသည် <id>၊ <n> သို့မဟုတ် … ဖြင့် ပြထားသော ပြောင်းလဲနိုင်သည့် အပိုင်းများမှလွဲ၍ တည်ငြိမ်သည်။ |
error.code | string | null | OpenAI format ၏ အချို့အမှားများတွင် ပါသည်- 401 ပါသော invalid_api_key၊ 413 ပါသော request_too_large၊ စကားပြောဆိုမှု ရှည်လွန်းလျှင် context_length_exceeded၊ မဟုတ်လျှင် null သို့မဟုတ် မပါပါ။ status နှင့် error.type ကို အရင်ကြည့်ပြီး ဆုံးဖြတ်ပါ၊ code ကို အပိုအချက်အလက်အဖြစ် သဘောထားပါ။ |
error.param | null | code ပါသော အမှားများတွင်ပင် ပါသည်။ အမြဲတမ်း null။ |
type | string | Anthropic format တွင်သာ။ အမြဲတမ်း error။ |
Error types
| Type | Statuses | ဖော်ပြချက် |
|---|---|---|
invalid_request_error | 400, 413, 415, 422 | request ကိုယ်တိုင် မှားနေသည်- field၊ body၊ model id သို့မဟုတ် model လက်မခံသော input။ ပြန်မပို့မီ request ကို ပြောင်းပါ။ |
authentication_error | 401 | API key မပါ၊ မသိ သို့မဟုတ် ပယ်ဖျက်ထားသည်။ |
not_found_error | 404 | path မရှိပါ။ |
rate_limit_error | 429 | လက်ကျန်ငွေ က request ကို မဖုံးလွှမ်းနိုင်၊ flood protection က ရပ်တန့်ခဲ့၊ သို့မဟုတ် model အလုပ်များနေသည်။ မက်ဆေ့ချ်က ဘယ်ဟာဆိုတာ ပြောပြသည်။ |
api_error | 500, 502, 503, 504 | request သည် မှန်ကန်ပြီး ကျရှုံးမှုသည် ကျွန်ုပ်တို့ဘက်တွင် ဖြစ်သည်။ ပြန်ပို့ပါ။ |
server_error | 500 | request သည် မှန်ကန်ပြီး ကျရှုံးမှုသည် ကျွန်ုပ်တို့ဘက်တွင် ဖြစ်သည်။ ပြန်ပို့ပါ။ |
Status codes
| Status | Type | မက်ဆေ့ချ် | ဖြစ်ပေါ်ချိန် |
|---|---|---|---|
400 | invalid_request_error | Failed to parse the request body as JSON: … | body သည် မှန်ကန်သော JSON မဟုတ်ပါ။ |
400 | invalid_request_error | unknown model: <id> | model သည် ထုတ်ပြန်ထားသော id မဟုတ်ပါ။ /v1/responses တွင် model မပါလျှင်လည်း ဖြစ်သည်။ |
400 | invalid_request_error | No user message provided | Shannon အဆင့်များ- request တွင် user စာသား မပါပါ။ |
400 | invalid_request_error | <id> does not accept image input | ပုံ input မရှိသော host လုပ်ထားသည့် open-weight model သို့ ပုံတစ်ပုံ ပို့ခဲ့သည်။ |
400 | invalid_request_error | audio and video input are not supported by any hosted model | အသံ သို့မဟုတ် ဗီဒီယို အစိတ်အပိုင်းကို host လုပ်ထားသော open-weight model သို့ ပို့ခဲ့သည်။ |
400 | invalid_request_error | <id> does not accept response_format | structured output မရှိသော host လုပ်ထားသည့် open-weight model ထံမှ ၎င်းကို တောင်းဆိုခဲ့သည်။ |
400 | invalid_request_error | <id> does not accept response_format json_schema; use json_object | json_object ကိုသာ လက်ခံသော host လုပ်ထားသည့် open-weight model သို့ JSON schema တစ်ခု ပို့ခဲ့သည်။ |
400 | invalid_request_error | unknown reasoning effort '<value>'; expected off, low, medium or high | reasoning effort သည် စာရင်းပြထားသော တန်ဖိုးများထဲမှ တစ်ခု မဟုတ်ပါ။ Host လုပ်ထားသော open-weight models များ။ |
400 | invalid_request_error | count_tokens is available for the hosted open models; unknown model: <id> | host လုပ်ထားသော open-weight models မဟုတ်သော model အတွက် token ရေတွက်ရန် တောင်းဆိုခဲ့သည်။ |
401 | authentication_error | Missing authentication | API key မပို့ခဲ့ပါ။ |
401 | authentication_error | Invalid API key | key ကို မသိ၊ ပိုအသစ်တစ်ခုဖြင့် အစားထိုးခံရ သို့မဟုတ် ပယ်ဖျက်ခံရသည်။ |
404 | not_found_error | Not Found | path မရှိပါ၊ ဥပမာ /v1/embeddings။ |
405 | api_error | Method Not Allowed | path ရှိသော်လည်း ဤ HTTP method ဖြင့် မရှိပါ။ |
413 | invalid_request_error | Failed to buffer the request body: … | request body သည် 32 MiB ထက် ပိုကြီးသည်။ |
415 | invalid_request_error | Expected request with `Content-Type: application/json` | Content-Type header မပါ သို့မဟုတ် application/json မဟုတ်ပါ။ |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | messages ကဲ့သို့ လိုအပ်သော field မပါ၊ သို့မဟုတ် field တစ်ခု၏ JSON type မှားနေသည်။ |
429 | rate_limit_error | Quota exceeded. Upgrade your plan at shannon-ai.com/plan | request ၏ output limit သည် သင့် လက်ကျန်ငွေ ၏ ကျန်ရှိသည်ထက် ပိုကြီးသည်။ |
429 | rate_limit_error | Too many requests. Retry in <n>s. | Flood protection- သင့် account တွင် တစ်မိနစ်အတွင်း request 120 ထက်ပို၍ ပို့ခဲ့သည်။ <n> သည် မိနစ်ပြီးဆုံးသည်အထိ စက္ကန့်အရေအတွက် ဖြစ်သည်။ |
429 | rate_limit_error | Shannon Coder call quota reached. Upgrade your plan at shannon-ai.com/plan | /v1/chat/completions နှင့် /v1/messages ပေါ်ရှိ shannon-coder-1- လက်ရှိ 4 နာရီ window အတွက် သင့် plan ၏ Shannon Coder calls ကုန်သွားပြီ။ |
429 | rate_limit_error | Shannon routes are temporarily busy. Please retry. | ယခုအချိန်တွင် model အလုပ်များနေသည်။ |
500 | server_error | The model backend failed to answer. Please retry. | model သည် ပြန်ကြားချက်ကို မထုတ်ပေးခဲ့ပါ။ |
500 | server_error | An internal error occurred. Please retry. | request ကို ပြင်ဆင်နေစဉ် အတွင်းပိုင်း ချို့ယွင်းမှု ဖြစ်ပေါ်ခဲ့သည်။ |
502 | api_error | The model backend failed to answer. Please retry. | model သည် ပြန်ကြားချက်ကို မထုတ်ပေးခဲ့ပါ။ Shannon 3 မိသားစုနှင့် host လုပ်ထားသော open-weight models များ။ |
503 | api_error | Could not verify your quota right now. Please retry. | ယခုအချိန်တွင် သင့် လက်ကျန်ငွေ ကို ဖတ်၍မရပါ။ |
503 | api_error | token counting is temporarily unavailable for this model | ယခုအချိန်တွင် ဤ model အတွက် token ရေတွက်ခြင်း မရနိုင်ပါ။ |
504 | api_error | upstream timed out | host လုပ်ထားသော open-weight model တစ်ခုသည် stream မလုပ်သော request ၏ အချိန်ကန့်သတ်ချက်ထက် ပိုကြာခဲ့သည်။ |
စစ်ဆေးမှုများကို ဤအစီအစဉ်ဖြင့် လုပ်သည်- API key၊ request body၊ model id၊ flood protection၊ လက်ကျန်ငွေ။ ထို့ကြောင့် key မှားပြီး body လည်း မှားသော request သည် 401 ကို ပြန်ပေးသည်။ 4xx status ဖြင့် ငြင်းပယ်ခံရသော request ကို ကျသင့်ငွေ မယူပါ။
လက်ကျန်ငွေနှင့် flood protection အကြောင်း
request တိုင်းကို ညီတူညီမျှ ဝန်ဆောင်မှုပေးသည်- rate tier မရှိ၊ သီးခြား API quota လည်း မရှိပါ။ သို့သော် အရာနှစ်ခုသည် 429 ဖြင့် ဖြေနိုင်ပြီး နှစ်ခုလုံးတွင် type rate_limit_error ဖြစ်သည်။ မက်ဆေ့ချ်က ခွဲခြားပေးသည်။
လက်ကျန်ငွေ
သင့် လက်ကျန်ငွေ မှာ ယနေ့အတွက် plan allowance ဖြစ်ပြီး 00:00 UTC တွင် ပြန်စကာ ၎င်းပြီးနောက် ဝယ်ယူထားသော credit ဖြစ်သည်။ Chat နှင့် API တို့သည် တူညီသော လက်ကျန်ငွေ မှ နုတ်ယူသည်။
request တိုင်းသည် ၎င်း၏ output limit ကို အရင်ဖယ်ထားသည်- max_tokens၊ သို့မဟုတ် /v1/responses တွင် max_output_tokens၊ နှစ်ခုလုံး မပို့လျှင် 4,096။ ပြန်ကြားချက် ပြီးမြောက်သောအခါ ဖယ်ထားသောပမာဏကို request သုံးခဲ့သည့်ပမာဏဖြင့် အစားထိုးသည်။ ပမာဏသည် ကျန်ရှိသော လက်ကျန်ငွေ ထဲ မဆန့်လျှင် model မစတင်မီ request သည် Quota exceeded ကို ပြန်ပေးသည်။
တစ်ပြိုင်နက် run နေသော request များသည် တစ်ခုချင်းစီ ၎င်းတို့၏ ကိုယ်ပိုင်ပမာဏကို ဖယ်ထားသည်။ ထို့ကြောင့် max_tokens မြင့်သော အပြိုင် request များစွာသည် ပြန်ကြားချက်တစ်ခုစီ တိုနေသော်လည်း လက်ကျန်ငွေ ကို ခဏတာ ကုန်စေနိုင်သည်။ max_tokens နိမ့်လျှင် ဖယ်ထားမှုနည်းသည်။
Flood protection
Flood protection သည် account တစ်ခုလျှင် တစ်မိနစ်ကို request 120 ခုအထိ ခွင့်ပြုသည်။ key ကို မဟုတ်ဘဲ account ကို /v1/chat/completions၊ /v1/responses နှင့် /v1/messages ပေါင်းပြီး ရေတွက်သည်။ မိနစ်သည် သင်၏ ပထမဆုံး request မှ စတင်သည်။
ထိုမိနစ်အတွင်း request အမှတ် 121 သည် Too many requests. Retry in <n>s. ကို ပြန်ပေးပြီး ကုန်ကျစရိတ် မရှိပါ။ ငြင်းပယ်ခံရသော request များကိုလည်း ရေတွက်သဖြင့် မစောင့်ဘဲ ပြန်ပို့သော client သည် မိနစ်ပြီးသည်အထိ ဆက်လက် ငြင်းပယ်ခံရမည်။ မက်ဆေ့ချ်ပါ <n> စက္ကန့်ကို စောင့်ပါ။
အပြိုင်ပို့သော request များကို အပြိုင်ဖြစ်သည့်အတွက် မငြင်းပယ်ပါ။ ၎င်းတို့သည် တန်းစီစောင့်ပြီး အလှည့်ကျ ဖြေကြားခံရသည်။
GET /v1/models နှင့် token ရေတွက်သော endpoint များကို မရေတွက်ပါ။
ကန့်သတ်ချက်များနှင့် လက်ကျန်ငွေ အကြောင်း ထပ်မံသိရန်၊ သင့်ကိုယ်ပိုင် လက်ကျန်ငွေ နှင့် အသုံးပြုမှုကို မည်သည့်နေရာတွင် ကြည့်နိုင်သည်ကို သိရန်- ကန့်သတ်ချက်များနှင့် လက်ကျန်ငွေ Keys & usage
ပြန်ကြိုးစားသင့်သည့်အရာများ
တူညီသော request သည် အောင်မြင်နိုင်မှသာ ပြန်ပို့ပါ။ OpenAI နှင့် Anthropic SDK များသည် 429 နှင့် 5xx ပြန်ကြားချက်များကို ကိုယ်တိုင် နှစ်ကြိမ် ပြန်ကြိုးစားပြီးသား ဖြစ်သည်၊ ပြောင်းလိုပါက max_retries (JavaScript တွင် maxRetries) ကို သတ်မှတ်ပါ။
| အမှား | ပြန်ပို့ရမလား? | လုပ်ပုံ |
|---|---|---|
400, 404, 405, 413, 415, 422 | မပို့ပါနှင့်။ request ကို အရင်ပြင်ပါ။ | မက်ဆေ့ချ်တွင် မှားနေသော field၊ model သို့မဟုတ် input ကို အမည်ပေးထားသည်။ |
401 | မပို့ပါနှင့်။ မှန်ကန်သော key ကို သုံးပါ။ | Keys & usage စာမျက်နှာတွင် key ကို ဖန်တီးပါ သို့မဟုတ် အစားထိုးပါ။ |
429 Too many requests | ရမည်၊ စောင့်ပြီးနောက်။ | မက်ဆေ့ချ်ပါ စက္ကန့်အရေအတွက်ကို စောင့်ပြီးနောက် တူညီသော request ကို ပို့ပါ။ |
429 Quota exceeded | ယခုအတိုင်း မဟုတ်ပါ။ | max_tokens ကို လျှော့ပါ၊ 00:00 UTC ရှိ နေ့စဉ် reset ကို စောင့်ပါ၊ သို့မဟုတ် credit ထည့်ပါ။ |
429 Shannon Coder call quota reached | ရမည်၊ နောက် window တွင်။ | window များသည် 00:00 UTC မှ စ၍ 4 နာရီတိုင်း စတင်သည်။ အခြား model က ချက်ချင်း ဖြေပေးသည်။ |
429 … temporarily busy | ပြန်ပို့ရမည်။ | စက္ကန့်အနည်းငယ် စောင့်ပါ၊ နောက်ထပ်ကြိုးစားတိုင်း ပိုကြာကြာ စောင့်ပါ။ |
500, 502, 503 | ပြန်ပို့ရမည်။ | 1 စက္ကန့်စောင့်ပါ၊ ထို့နောက် 2၊ 4 နှင့် 8။ အကြိမ်အနည်းငယ်ပြီးလျှင် ရပ်ပြီး request id ကို သတင်းပို့ပါ။ |
504 | ပြန်ပို့ရမည်။ | stream ကို true ဟု သတ်မှတ်ပြီး ပို့ပါ- အချိန်ကန့်သတ်ချက်သည် stream မလုပ်သော ပြန်ကြားချက်များအတွက်သာ သက်ရောက်သည်။ |
ဤဇယားကို လိုက်နာသော retry loop-
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); Stream အတွင်းရှိ အမှားများ
stream သည် ပထမ byte မှစ၍ status 200 ဖြစ်သည်။ ထို့နောက် ဖြစ်ပေါ်သော ကျရှုံးမှုသည် stream အတွင်း event အဖြစ် ရောက်လာသည်- /v1/chat/completions တွင် error member ပါသော chunk၊ /v1/messages တွင် error event၊ /v1/responses တွင် response.failed သို့မဟုတ် error event။ stream ၏ အဆုံးသတ် events များ ဆက်လက်လာသေးသည်။ ထိုပြန်ကြားချက်ကို ကျရှုံးသည်ဟု သဘောထားပြီး 5xx status ကဲ့သို့ request ကို ပြန်ပို့ပါ။
model ၏ context window ထက် ရှည်သော စကားပြောဆိုမှုကို ဤနည်းဖြင့် ဖော်ပြပြီး type invalid_request_error နှင့် code context_length_exceeded ပါသည်။ ပြန်မပို့မီ စကားပြောဆိုမှုကို တိုစေပါ။
တိကျသော frame များကို streaming တွင် ဖော်ပြထားသည်။ စตรีမင်း
Request ids
အမှားများအပါအဝင် ပြန်ကြားချက်တိုင်းတွင် x-request-id header ပါသည်။ ၎င်းသည် သင်၏ ကိုယ်ပိုင် x-request-id header တွင် ပို့ခဲ့သော တန်ဖိုး၊ မပို့ခဲ့လျှင် hexadecimal စာလုံး 12 လုံး ဖြစ်သည်။ ၎င်းကို log မှတ်ထားပြီး request တစ်ခုအတွက် အကူအညီတောင်းသောအခါ ကိုးကားပါ။