Fitantanana hadisoana
Ny fangatahana tsy nahomby dia mamerina code status HTTP sy body JSON misy type hadisoana sy hafatra. Ity pejy ity dia mitanisa ny status rehetra, mampiseho ny body hadisoana amin'ny endrika API tsirairay ary milaza hoe iza amin'ireo hadisoana no ilaina alefa indray.
Ny SDK dia manangana object hadisoana mitondra ny code status sy ny saha ao amin'ny body. Ity fangatahana ity dia manonona model tsy misy:
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"}]
}' Mamerina status 400 izy ary ity body ity:
{
"error": {
"type": "invalid_request_error",
"message": "unknown model: no-such-model"
}
} Vatan'ny hadisoana
Misy body roa. Ny /v1/messages dia mamaly amin'ny endrika Anthropic; ny endpoint hafa rehetra dia mamaly amin'ny endrika OpenAI. Amin'ireo roa ireo, ny antsipiriany dia ao amin'ny error.type sy error.message, ka mpamaky iray no ampy ho an'ny endpoint rehetra.
{
"error": {
"type": "authentication_error",
"message": "Invalid API key",
"code": "invalid_api_key",
"param": null
}
} {
"type": "error",
"error": {
"type": "authentication_error",
"message": "Invalid API key"
}
} | Saha | Karazana | Famaritana |
|---|---|---|
error.type | string | Ny karazan'ny hadisoana. Jereo ny Karazan'ny hadisoana etsy ambany. |
error.message | string | Fehezanteny ho an'olona. Miorina ny soratra ao amin'ny tabilao etsy ambany afa-tsy ny ampahany miovaova, aseho amin'ny <id>, <n> na …. |
error.code | string | null | Misy amin'ny hadisoana sasany amin'ny endrika OpenAI: invalid_api_key miaraka amin'ny 401, request_too_large miaraka amin'ny 413, context_length_exceeded rehefa lava loatra ny resaka, ankoatra izay dia null na tsy misy. Ny status sy ny error.type aloha no jerena, ary ny code dia fanampiny fotsiny. |
error.param | null | Misy amin'ny hadisoana mitovy amin'ny code. Foana null. |
type | string | Endrika Anthropic ihany. Foana error. |
Karazan'ny hadisoana
| Karazana | Status | Famaritana |
|---|---|---|
invalid_request_error | 400, 413, 415, 422 | Diso ny fangatahana mihitsy: saha, body, id ny model na input tsy raisin'ny model. Ovay ny fangatahana alohan'ny handefasanao indray. |
authentication_error | 401 | Tsy misy, tsy fantatra na nofoanana ny API key. |
not_found_error | 404 | Tsy misy ny lalana. |
rate_limit_error | 429 | Tsy ampy ny balance hanaovana ny fangatahana, nosakanan'ny flood protection izany, na be atao ny model. Ny hafatra no milaza hoe iza. |
api_error | 500, 502, 503, 504 | Tsara ny fangatahana ary ny fahatafintohinana dia any amintsika. Alefaso indray. |
server_error | 500 | Tsara ny fangatahana ary ny fahatafintohinana dia any amintsika. Alefaso indray. |
Code status
| Status | Karazana | Hafatra | Rahoviana |
|---|---|---|---|
400 | invalid_request_error | Failed to parse the request body as JSON: … | Tsy JSON mety ny body. |
400 | invalid_request_error | unknown model: <id> | Tsy id navoaka ny model. Amin'ny /v1/responses koa rehefa tsy misy ny model. |
400 | invalid_request_error | No user message provided | Tier Shannon: tsy misy soratra avy amin'ny mpampiasa ny fangatahana. |
400 | invalid_request_error | <id> does not accept image input | Nalefa sary tamin'ny model open-weight hosted tsy mandray sary. |
400 | invalid_request_error | audio and video input are not supported by any hosted model | Nalefa tamin'ny model open-weight hosted ny ampahany audio na video. |
400 | invalid_request_error | <id> does not accept response_format | Nangatahana structured output tamin'ny model open-weight hosted tsy manana izany. |
400 | invalid_request_error | <id> does not accept response_format json_schema; use json_object | Nalefa JSON schema tamin'ny model open-weight hosted izay mandray json_object ihany. |
400 | invalid_request_error | unknown reasoning effort '<value>'; expected off, low, medium or high | Tsy iray amin'ny sanda voatanisa ny reasoning effort. Model open-weight hosted. |
400 | invalid_request_error | count_tokens is available for the hosted open models; unknown model: <id> | Nangatahana ny fanisana token ho an'ny model ivelan'ny model open-weight hosted. |
401 | authentication_error | Missing authentication | Tsy nandefa API key. |
401 | authentication_error | Invalid API key | Tsy fantatra ny key, nosoloina key vaovao na nofoanana. |
404 | not_found_error | Not Found | Tsy misy ny lalana, ohatra /v1/embeddings. |
405 | api_error | Method Not Allowed | Misy ny lalana nefa tsy amin'ity fomba HTTP ity. |
413 | invalid_request_error | Failed to buffer the request body: … | Lehibe noho ny 32 MiB ny body ny fangatahana. |
415 | invalid_request_error | Expected request with `Content-Type: application/json` | Tsy misy ny header Content-Type na tsy application/json izy. |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | Tsy misy saha ilaina, toy ny messages, na diso ny karazana JSON an'ny saha iray. |
429 | rate_limit_error | Quota exceeded. Upgrade your plan at shannon-ai.com/plan | Lehibe noho ny sisa amin'ny balance-nao ny fetran'ny output an'ny fangatahana. |
429 | rate_limit_error | Too many requests. Retry in <n>s. | Flood protection: mihoatra ny fangatahana 120 ao anatin'ny minitra iray amin'ny kaonty. Ny <n> dia ny isan'ny segondra mandra-pahatapitry ny minitra. |
429 | rate_limit_error | Shannon Coder call quota reached. Upgrade your plan at shannon-ai.com/plan | shannon-coder-1 amin'ny /v1/chat/completions sy /v1/messages: lany ny antso Shannon Coder an'ny plan-nao amin'ny varavarankely 4 ora ankehitriny. |
429 | rate_limit_error | Shannon routes are temporarily busy. Please retry. | Be atao ny model amin'izao fotoana izao. |
500 | server_error | The model backend failed to answer. Please retry. | Tsy namoaka valiny ny model. |
500 | server_error | An internal error occurred. Please retry. | Tsy fahombiazana anatiny teo am-pamahanana ny fangatahana. |
502 | api_error | The model backend failed to answer. Please retry. | Tsy namoaka valiny ny model. Fianakavian'i Shannon 3 sy model open-weight hosted. |
503 | api_error | Could not verify your quota right now. Please retry. | Tsy azo novaky amin'izao fotoana izao ny balance-nao. |
503 | api_error | token counting is temporarily unavailable for this model | Tsy azo atao amin'izao fotoana izao ny fanisana token ho an'ity model ity. |
504 | api_error | upstream timed out | Naharitra lavitra noho ny fetra fotoana ny model open-weight hosted amin'ny fangatahana tsy streamed. |
Atao araka ity filaharana ity ny fanamarinana: API key, body ny fangatahana, id ny model, flood protection, balance. Noho izany, ny fangatahana misy key diso sy body diso dia mamerina 401. Ny fangatahana nolavina tamin'ny status 4xx dia tsy vidiana.
Balance sy flood protection
Ny fangatahana rehetra dia tanterahina mitovy: tsy misy rate tier ary tsy misy quota API mitokana. Misy zavatra roa mbola mety mamaly 429, samy manana ny type rate_limit_error. Ny hafatra no manavaka azy.
Balance
Ny balance-nao dia ny allowance an'ny plan androany, izay averina amin'ny 00:00 UTC, ary raha lany dia ny credit novidinao. Ny chat sy ny API dia mamoaka avy amin'ny balance iray ihany.
Ny fangatahana tsirairay dia mametraka voalohany ny fetran'ny output-ny: max_tokens, na max_output_tokens amin'ny /v1/responses, 4,096 raha tsy mandefa roa ianao. Rehefa vita ny valiny, ny habetsaky ny napetraka dia soloina ny nampiasain'ny fangatahana. Raha tsy mifanaraka amin'ny balance sisa ny habetsaka, ny fangatahana dia mamerina Quota exceeded alohan'ny hanombohan'ny model.
Ny fangatahana mandeha miaraka dia samy mametraka ny habetsahany. Noho izany, fangatahana maro miaraka misy max_tokens avo dia mety hampitsahatra vetivety ny balance, na dia fohy aza ny valiny tsirairay. Ny max_tokens ambany kokoa dia mametraka kely kokoa.
Flood protection
Ny flood protection dia mamela fangatahana 120 isa-minitra isaky ny kaonty. Ny kaonty no ianarana, fa tsy ny key, amin'ny /v1/chat/completions, /v1/responses ary /v1/messages miaraka. Manomboka amin'ny fangatahana voalohany ny minitra.
Ny fangatahana faha-121 ao anatin'io minitra io dia mamerina Too many requests. Retry in <n>s. ary tsy misy vidiny. Ianarana koa ny fangatahana nolavina, ka ny client izay mandefa indray tsy miandry dia mijanona nolavina mandra-pahatapitry ny minitra. Andraso ny <n> segondra voalaza ao amin'ny hafatra.
Ny fangatahana alefa miaraka dia tsy lavina noho izy miaraka. Miandry ao anaty filaharana izy ireo ary valiana isam-bava.
Ny GET /v1/models sy ny endpoint fanisana token dia tsy ianarana.
Fanampiana bebe kokoa momba ny fetra sy ny balance, ary izay hijerena ny balance sy ny fampiasanao: Fetra sy balance Keys & usage
Izay hatao retry
Alefaso indray ny fangatahana rehefa mety hahomby ny fangatahana mitovy ihany. Ny SDK OpenAI sy Anthropic dia efa manao retry indroa ho azy amin'ny valiny 429 sy 5xx; apetraho ny max_retries (maxRetries amin'ny JavaScript) raha te-hanova izany.
| Hadisoana | Alefa indray? | Ahoana |
|---|---|---|
400, 404, 405, 413, 415, 422 | Tsia. Ahitsio aloha ny fangatahana. | Ny hafatra dia manonona ny saha, ny model na ny input diso. |
401 | Tsia. Ampiasao key mety. | Mamorona na solòy ny key ao amin'ny pejy Keys & usage. |
429 Too many requests | Eny, aorian'ny fiandrasana. | Andraso ny isan'ny segondra ao amin'ny hafatra, avy eo alefaso ny fangatahana mitovy. |
429 Quota exceeded | Tsy araka izao. | Ahenoy ny max_tokens, andraso ny reset isan'andro amin'ny 00:00 UTC, na ampio credit. |
429 Shannon Coder call quota reached | Eny, amin'ny varavarankely manaraka. | Manomboka isaky ny 4 ora ny varavarankely, hatrany amin'ny 00:00 UTC. Model hafa dia mamaly avy hatrany. |
429 … temporarily busy | Eny. | Andraso segondra vitsivitsy, avy eo lava kokoa isaky ny fanandramana manaraka. |
500, 502, 503 | Eny. | Andraso 1 segondra, avy eo 2, 4 ary 8. Ajanony aorian'ny fanandramana vitsivitsy ary ambarao ny id an'ny fangatahana. |
504 | Eny. | Alefaso miaraka amin'ny stream voapetraka amin'ny true: ny fetra fotoana dia mihatra amin'ny valiny tsy streamed. |
Retry loop manaraka ity tabilao ity:
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); Hadisoana ao anaty stream
Ny stream dia manana status 200 hatrany amin'ny byte voalohany. Ny fahatafintohinana aorian'izay dia tonga ho event ao anaty stream: chunk misy mpikambana error amin'ny /v1/chat/completions, event error amin'ny /v1/messages, event response.failed na error amin'ny /v1/responses. Mbola manaraka ireo event famaranana ny stream. Heverina ho tsy nahomby ny valiny toy izany ary alefaso indray ny fangatahana, toy ny amin'ny status 5xx.
Ny resaka lava noho ny context window an'ny model dia ambara amin'izany fomba izany, miaraka amin'ny type invalid_request_error sy ny code context_length_exceeded. Ahenoy ny resaka alohan'ny handefasanao indray.
Ny frame marina dia voasoratra miaraka amin'ny streaming. Fandefasana mivantana
Id an'ny fangatahana
Ny valiny rehetra, anisan'izany ny hadisoana, dia manana header x-request-id. Io dia ny sanda nalefanao tao amin'ny header x-request-id anao, na tarehintsoratra hexadecimal 12 raha tsy nandefa ianao. Tehirizo ao amin'ny log izy, ary lazao rehefa mangataka fanampiana momba ny fangatahana.