ભૂલ સંભાળ
નિષ્ફળ જતી રિક્વેસ્ટ HTTP સ્ટેટસ કોડ અને એરર પ્રકાર તથા મેસેજ સાથેનો JSON બોડી પાછો આપે છે. આ પેજ દરેક સ્ટેટસ યાદી કરે છે, દરેક API ફોર્મેટનો એરર બોડી બતાવે છે અને જણાવે છે કે કઈ એરર્સ ફરી મોકલવા યોગ્ય છે.
SDK એવો એરર ઓબ્જેક્ટ ઉઠાવે છે જે સ્ટેટસ કોડ અને બોડીના ફીલ્ડ્સ લઈ જાય છે. આ રિક્વેસ્ટ એવા મોડેલનું નામ આપે છે જે અસ્તિત્વમાં નથી:
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"}]
}' તે સ્ટેટસ 400 અને આ બોડી પાછું આપે છે:
{
"error": {
"type": "invalid_request_error",
"message": "unknown model: no-such-model"
}
} એરર બોડી
બે બોડી છે. /v1/messages Anthropic ફોર્મેટમાં જવાબ આપે છે; બીજા દરેક એન્ડપોઇન્ટ OpenAI ફોર્મેટમાં જવાબ આપે છે. બંનેમાં વિગતો error.type અને error.message માં હોય છે, તેથી એક જ રીડર બધા એન્ડપોઇન્ટ્સ માટે ચાલે છે.
{
"error": {
"type": "authentication_error",
"message": "Invalid API key",
"code": "invalid_api_key",
"param": null
}
} {
"type": "error",
"error": {
"type": "authentication_error",
"message": "Invalid API key"
}
} | ફીલ્ડ | પ્રકાર | વર્ણન |
|---|---|---|
error.type | string | એરરનો પ્રકાર. નીચે એરર પ્રકારો જુઓ. |
error.message | string | માણસ માટેનું વાક્ય. નીચેના ટેબલમાંના ટેક્સ્ટ, <id>, <n> અથવા … તરીકે બતાવેલા ચલ ભાગો સુધી, સ્થિર છે. |
error.code | string | null | OpenAI ફોર્મેટની કેટલીક એરર્સ પર હાજર: 401 સાથે invalid_api_key, 413 સાથે request_too_large, વાતચીત બહુ લાંબી હોય ત્યારે context_length_exceeded, અન્યથા null અથવા ગેરહાજર. પહેલાં સ્ટેટસ અને error.type પર નિર્ણય લો અને code ને વધારાનું ગણો. |
error.param | null | code જેવી જ એરર્સ પર હાજર. હંમેશા null. |
type | string | માત્ર Anthropic ફોર્મેટ. હંમેશા error. |
એરર પ્રકારો
| પ્રકાર | સ્ટેટસ | વર્ણન |
|---|---|---|
invalid_request_error | 400, 413, 415, 422 | રિક્વેસ્ટ પોતે ખોટી છે: કોઈ ફીલ્ડ, બોડી, મોડેલ id અથવા મોડેલ ન લેતું ઇનપુટ. ફરી મોકલતાં પહેલાં રિક્વેસ્ટ બદલો. |
authentication_error | 401 | API કી ખૂટે છે, અજાણી છે અથવા રદ કરેલી છે. |
not_found_error | 404 | પાથ અસ્તિત્વમાં નથી. |
rate_limit_error | 429 | બેલેન્સ રિક્વેસ્ટને આવરી લેતું નથી, flood protection એ તેને અટકાવી, અથવા મોડેલ વ્યસ્ત છે. મેસેજ જણાવે છે કયું. |
api_error | 500, 502, 503, 504 | રિક્વેસ્ટ બરાબર હતી અને નિષ્ફળતા અમારી બાજુએ છે. તે ફરી મોકલો. |
server_error | 500 | રિક્વેસ્ટ બરાબર હતી અને નિષ્ફળતા અમારી બાજુએ છે. તે ફરી મોકલો. |
સ્ટેટસ કોડ
| સ્ટેટસ | પ્રકાર | મેસેજ | ક્યારે |
|---|---|---|---|
400 | invalid_request_error | Failed to parse the request body as JSON: … | બોડી માન્ય JSON નથી. |
400 | invalid_request_error | unknown model: <id> | model પ્રકાશિત id નથી. /v1/responses પર model ખૂટતું હોય ત્યારે પણ. |
400 | invalid_request_error | No user message provided | Shannon ટિયર: રિક્વેસ્ટમાં કોઈ user ટેક્સ્ટ નથી. |
400 | invalid_request_error | <id> does not accept image input | ઇમેજ ઇનપુટ વિનાના હોસ્ટેડ open-weight મોડેલને ઇમેજ મોકલવામાં આવી. |
400 | invalid_request_error | audio and video input are not supported by any hosted model | હોસ્ટેડ open-weight મોડેલને ઓડિયો અથવા વિડિયો ભાગ મોકલવામાં આવ્યો. |
400 | invalid_request_error | <id> does not accept response_format | સ્ટ્રક્ચર્ડ આઉટપુટ વિનાના હોસ્ટેડ open-weight મોડેલ પાસે તે માંગવામાં આવ્યું. |
400 | invalid_request_error | <id> does not accept response_format json_schema; use json_object | ફક્ત json_object લેતા હોસ્ટેડ open-weight મોડેલને JSON schema મોકલવામાં આવી. |
400 | invalid_request_error | unknown reasoning effort '<value>'; expected off, low, medium or high | reasoning effort યાદી કરેલી કિંમતોમાંની કોઈ નથી. હોસ્ટેડ open-weight મોડેલ્સ. |
400 | invalid_request_error | count_tokens is available for the hosted open models; unknown model: <id> | હોસ્ટેડ open-weight મોડેલ્સની બહારના મોડેલ માટે ટોકન ગણતરી માંગવામાં આવી. |
401 | authentication_error | Missing authentication | કોઈ API કી મોકલાઈ નથી. |
401 | authentication_error | Invalid API key | કી અજાણી છે, નવી કીથી બદલાઈ ગઈ છે, અથવા રદ કરેલી છે. |
404 | not_found_error | Not Found | પાથ અસ્તિત્વમાં નથી, ઉદાહરણ તરીકે /v1/embeddings. |
405 | api_error | Method Not Allowed | પાથ અસ્તિત્વમાં છે પણ આ HTTP મેથડ સાથે નહીં. |
413 | invalid_request_error | Failed to buffer the request body: … | રિક્વેસ્ટ બોડી 32 MiB કરતાં મોટો છે. |
415 | invalid_request_error | Expected request with `Content-Type: application/json` | Content-Type હેડર ખૂટે છે અથવા application/json નથી. |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | messages જેવું જરૂરી ફીલ્ડ ખૂટે છે, અથવા કોઈ ફીલ્ડનો JSON પ્રકાર ખોટો છે. |
429 | rate_limit_error | Quota exceeded. Upgrade your plan at shannon-ai.com/plan | રિક્વેસ્ટની આઉટપુટ મર્યાદા તમારા બેલેન્સમાં જે બાકી છે તેના કરતાં મોટી છે. |
429 | rate_limit_error | Too many requests. Retry in <n>s. | Flood protection: તમારા એકાઉન્ટ પર એક મિનિટમાં 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-કલાકની વિન્ડો માટે તમારા પ્લાનના Shannon Coder કોલ્સ વપરાઈ ગયા છે. |
429 | rate_limit_error | Shannon routes are temporarily busy. Please retry. | મોડેલ આ ક્ષણે વ્યસ્ત છે. |
500 | server_error | The model backend failed to answer. Please retry. | મોડેલે જવાબ આપ્યો નથી. |
500 | server_error | An internal error occurred. Please retry. | રિક્વેસ્ટ તૈયાર થતી વખતે આંતરિક નિષ્ફળતા. |
502 | api_error | The model backend failed to answer. Please retry. | મોડેલે જવાબ આપ્યો નથી. Shannon 3 ફેમિલી અને હોસ્ટેડ open-weight મોડેલ્સ. |
503 | api_error | Could not verify your quota right now. Please retry. | આ ક્ષણે તમારું બેલેન્સ વાંચી શકાયું નથી. |
503 | api_error | token counting is temporarily unavailable for this model | આ ક્ષણે આ મોડેલ માટે ટોકન ગણતરી ઉપલબ્ધ નથી. |
504 | api_error | upstream timed out | હોસ્ટેડ open-weight મોડેલે સ્ટ્રીમ ન થતી રિક્વેસ્ટની સમય મર્યાદા કરતાં વધુ સમય લીધો. |
તપાસ આ ક્રમમાં થાય છે: API કી, રિક્વેસ્ટ બોડી, મોડેલ id, flood protection, બેલેન્સ. તેથી ખોટી કી અને ખોટા બોડીવાળી રિક્વેસ્ટ 401 પાછું આપે છે. 4xx સ્ટેટસ સાથે નકારાયેલી રિક્વેસ્ટનો ચાર્જ લાગતો નથી.
બેલેન્સ અને ફ્લડ પ્રોટેક્શન
દરેક રિક્વેસ્ટ સમાન રીતે પીરસાય છે: કોઈ rate ટિયર નથી અને કોઈ અલગ API ક્વોટા નથી. છતાં બે બાબતો 429 થી જવાબ આપી શકે છે, બંને rate_limit_error પ્રકાર સાથે. મેસેજ તેમને અલગ પાડે છે.
બેલેન્સ
તમારું બેલેન્સ એટલે આજનું પ્લાન એલાઉન્સ, જે 00:00 UTC એ રીસેટ થાય છે, અને તેના પછી તમારું ખરીદેલ ક્રેડિટ. ચેટ અને API એ જ બેલેન્સમાંથી લે છે.
દરેક રિક્વેસ્ટ પહેલા તેની આઉટપુટ મર્યાદા અલગ રાખે છે: max_tokens, અથવા /v1/responses પર max_output_tokens, તમે બેમાંથી કંઈ ન મોકલો ત્યારે 4,096. જવાબ પૂરો થાય ત્યારે અલગ રાખેલી રકમ રિક્વેસ્ટે વાપરેલી રકમથી બદલાય છે. જો રકમ બાકી રહેલા બેલેન્સમાં ન સમાય, તો મોડેલ શરૂ થાય તે પહેલાં રિક્વેસ્ટ Quota exceeded પાછું આપે છે.
એક જ સમયે ચાલતી રિક્વેસ્ટ્સ દરેક પોતાની રકમ અલગ રાખે છે. તેથી ઊંચા max_tokens વાળી ઘણી સમાંતર રિક્વેસ્ટ્સ, દરેક જવાબ ટૂંકો હોય તો પણ, થોડા સમય માટે બેલેન્સ વાપરી નાખી શકે છે. ઓછું max_tokens ઓછું અલગ રાખે છે.
Flood protection
Flood protection એકાઉન્ટ દીઠ પ્રતિ મિનિટ 120 રિક્વેસ્ટની મંજૂરી આપે છે. તે /v1/chat/completions, /v1/responses અને /v1/messages ને ભેગા ગણીને એકાઉન્ટ ગણે છે, કીને નહીં. મિનિટ તમારી પહેલી રિક્વેસ્ટથી શરૂ થાય છે.
તે મિનિટની અંદર રિક્વેસ્ટ નંબર 121 Too many requests. Retry in <n>s. પાછું આપે છે અને તેનો કોઈ ખર્ચ નથી. નકારાયેલી રિક્વેસ્ટ્સ પણ ગણાય છે, તેથી રાહ જોયા વિના ફરી મોકલતો ક્લાયન્ટ મિનિટ પૂરી થાય ત્યાં સુધી નકારાતો રહે છે. મેસેજમાંના <n> સેકન્ડ રાહ જુઓ.
સમાંતર મોકલેલી રિક્વેસ્ટ્સ સમાંતર હોવાને કારણે નકારાતી નથી. તે કતારમાં રાહ જુએ છે અને વારા પ્રમાણે જવાબ મળે છે.
GET /v1/models અને ટોકન ગણતરી એન્ડપોઇન્ટ્સ ગણાતા નથી.
મર્યાદાઓ અને બેલેન્સ વિશે વધુ, અને તમારું પોતાનું બેલેન્સ અને વપરાશ ક્યાં જોવા: મર્યાદાઓ અને બેલેન્સ Keys & usage
શું ફરી અજમાવવું
રિક્વેસ્ટ ત્યારે જ ફરી મોકલો જ્યારે એ જ રિક્વેસ્ટ સફળ થઈ શકે. OpenAI અને Anthropic SDK 429 અને 5xx જવાબો પર પહેલેથી જાતે બે વાર ફરી પ્રયાસ કરે છે; તે બદલવા max_retries (JavaScript માં maxRetries) સેટ કરો.
| એરર | ફરી મોકલવી? | કેવી રીતે |
|---|---|---|
400, 404, 405, 413, 415, 422 | ના. પહેલા રિક્વેસ્ટ સુધારો. | મેસેજ ખોટું ફીલ્ડ, મોડેલ અથવા ઇનપુટ જણાવે છે. |
401 | ના. માન્ય કી વાપરો. | Keys & usage પેજ પર કી બનાવો અથવા બદલો. |
429 Too many requests | હા, રાહ જોયા પછી. | મેસેજમાં આપેલી સેકન્ડ જેટલી રાહ જુઓ, પછી એ જ રિક્વેસ્ટ મોકલો. |
429 Quota exceeded | જેમ છે તેમ નહીં. | max_tokens ઘટાડો, 00:00 UTC એ દૈનિક રીસેટની રાહ જુઓ, અથવા ક્રેડિટ ઉમેરો. |
429 Shannon Coder call quota reached | હા, આગલી વિન્ડોમાં. | વિન્ડો 00:00 UTC થી દર 4 કલાકે શરૂ થાય છે. બીજું મોડેલ તરત જવાબ આપે છે. |
429 … temporarily busy | હા. | થોડી સેકન્ડ રાહ જુઓ, પછી દરેક વધુ પ્રયાસે વધુ લાંબી. |
500, 502, 503 | હા. | 1 સેકન્ડ રાહ જુઓ, પછી 2, 4 અને 8. થોડા પ્રયાસો પછી અટકો અને રિક્વેસ્ટ id જણાવો. |
504 | હા. | stream ને true સેટ કરીને મોકલો: સમય મર્યાદા સ્ટ્રીમ ન થતા જવાબોને લાગુ પડે છે. |
આ ટેબલને અનુસરતો retry લૂપ:
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); સ્ટ્રીમની અંદરની એરર્સ
સ્ટ્રીમનું સ્ટેટસ પહેલા બાઇટથી 200 હોય છે. ત્યારપછીની નિષ્ફળતા સ્ટ્રીમની અંદર ઇવેન્ટ તરીકે આવે છે: /v1/chat/completions પર error સભ્ય ધરાવતો chunk, /v1/messages પર error ઇવેન્ટ, /v1/responses પર response.failed અથવા error ઇવેન્ટ. સ્ટ્રીમની અંતિમ ઇવેન્ટ્સ હજી પણ પછી આવે છે. આવા જવાબને નિષ્ફળ ગણો અને 5xx સ્ટેટસની જેમ રિક્વેસ્ટ ફરી મોકલો.
મોડેલની કોન્ટેક્સ્ટ વિન્ડો કરતાં લાંબી વાતચીત આ રીતે જણાવાય છે, પ્રકાર invalid_request_error અને કોડ context_length_exceeded સાથે. ફરી મોકલતાં પહેલાં વાતચીત ટૂંકી કરો.
ચોક્કસ ફ્રેમ્સ streaming સાથે વર્ણવેલી છે. સ્ટ્રીમિંગ
રિક્વેસ્ટ id
દરેક જવાબ, એરર્સ સહિત, x-request-id હેડર ધરાવે છે. તે તમે તમારા પોતાના x-request-id હેડરમાં મોકલેલી કિંમત છે, અથવા તમે કંઈ ન મોકલ્યું હોય ત્યારે 12 હેક્સાડેસિમલ અક્ષરો. તેને લોગ કરો, અને રિક્વેસ્ટ અંગે મદદ માંગો ત્યારે ટાંકો.