Skip to main content

Rerank API

This page documents the 3 gateway endpoints tagged rerank in the gateway's OpenAPI document. Every example targets a gateway on http://localhost:4000 and authenticates with the SPANWAY_API_KEY environment variable, which holds the master key or a virtual key that has access to the route. Request and response fields are taken from the schema the running gateway publishes at /openapi.json, and the same document is bundled with this site as openapi.json.

MethodPathSummary
POST/rerankRerank documents by relevance to a query
POST/v1/rerankRerank documents by relevance to a query
POST/v2/rerankRerank documents by relevance to a query

Endpoints​

POST /rerank​

Rerank documents by relevance to a query

Score each document against the query and return them ordered by relevance. top_n limits the results and return_documents includes the document text. The gateway serves one request shape for every rerank provider and prices the call per query or per token.

Request body (application/json, object):

FieldTypeRequiredDescription
modelstringYesModel
querystringYesQuery
top_ninteger or nullNoTop N
documentsarray of string or objectYesDocuments
rank_fieldsarray of string or nullNoRank Fields
return_documentsboolean or nullNoReturn Documents
max_chunks_per_docinteger or nullNoMax Chunks Per Doc
max_tokens_per_docinteger or nullNoMax Tokens Per Doc
instructionstring or nullNoInstruction
truncate_prompt_tokensinteger or nullNoTruncate Prompt Tokens
truncation_sideenum: "left", "right" or nullNoTruncation Side
max_tokens_per_queryinteger or nullNoMax Tokens Per Query

Example request:

curl -X POST "http://localhost:4000/rerank" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "my-rerank-model",
"query": "What is the capital of France?",
"documents": [
"Paris is the capital of France.",
"Berlin is the capital of Germany."
],
"top_n": 1
}'

Responses:

StatusDescriptionBody
200The documents ordered by relevance with a score for each.application/json object
400The request is invalid, for example a missing or unsupported parameter, a prompt longer than the model's context window, content a guardrail or the provider refused, or no deployment that satisfies the data policy of the key, team or organization (No deployment of model 'gpt-5.5' satisfies the data policy: ...). error.message says which.application/json ProxyError
401No valid key was sent, or the key has expired or is blocked.application/json ProxyError
403The key is valid but may not use this model, route or feature. A guardrail block is a 400, not a 403.application/json ProxyError
404The route or resource does not exist, or this key cannot see it. An unknown model name is a 400 (Invalid model name passed in model=...).application/json ProxyError
408The provider did not answer before the request timed out.application/json ProxyError
413The request body is larger than max_request_size_mb (100 MB by default), or an upload is larger than its own limit. The gateway refused it before reading the key. Docsapplication/json object
422The request body does not have the expected shape, or a budget (key, user, team, organization or end user) is used up: error.type is budget_exceeded. The budget status is spanway_settings.budget_exceeded_status_code, 422 by default. With access requests licensed, a key or team limit refusal also carries error.request_increase, which names the access request to file for a higher limit. Ask from an errorapplication/json ProxyError or HTTPValidationError
429Too many requests. A provider limit or a gateway limit (requests or tokens per minute, or parallel requests) refused the call, and error.type says which. A gateway limit also sends the RateLimit headers and x-spanway-rate-limited-by. Docs With access requests licensed, a key or team limit refusal also carries error.request_increase, which names the access request to file for a higher limit. Ask from an errorapplication/json ProxyError
500The gateway or the provider failed unexpectedly, or the gateway could not reach the provider. Retry, and quote the request id if it keeps failing.application/json ProxyError
502The provider, or a proxy in front of it, answered with something that is not a valid response.application/json ProxyError
503The provider is overloaded or unavailable. With Redis rate limiting, a 503 also means the gateway could not check the rate limit counters and refused the request, because fail_closed_rate_limit_enforcement is on. Docsapplication/json ProxyError

Response fields (200):

FieldTypeRequiredDescription
idstring or nullNoId
resultsarray of object or nullNoResults
metaobject or nullNo

POST /v1/rerank​

Rerank documents by relevance to a query

Score each document against the query and return them ordered by relevance. top_n limits the results and return_documents includes the document text. The gateway serves one request shape for every rerank provider and prices the call per query or per token.

Request body (application/json, object):

FieldTypeRequiredDescription
modelstringYesModel
querystringYesQuery
top_ninteger or nullNoTop N
documentsarray of string or objectYesDocuments
rank_fieldsarray of string or nullNoRank Fields
return_documentsboolean or nullNoReturn Documents
max_chunks_per_docinteger or nullNoMax Chunks Per Doc
max_tokens_per_docinteger or nullNoMax Tokens Per Doc
instructionstring or nullNoInstruction
truncate_prompt_tokensinteger or nullNoTruncate Prompt Tokens
truncation_sideenum: "left", "right" or nullNoTruncation Side
max_tokens_per_queryinteger or nullNoMax Tokens Per Query

Example request:

curl -X POST "http://localhost:4000/v1/rerank" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "my-rerank-model",
"query": "What is the capital of France?",
"documents": [
"Paris is the capital of France.",
"Berlin is the capital of Germany."
],
"top_n": 1
}'

Responses:

StatusDescriptionBody
200The documents ordered by relevance with a score for each.application/json object
400The request is invalid, for example a missing or unsupported parameter, a prompt longer than the model's context window, content a guardrail or the provider refused, or no deployment that satisfies the data policy of the key, team or organization (No deployment of model 'gpt-5.5' satisfies the data policy: ...). error.message says which.application/json ProxyError
401No valid key was sent, or the key has expired or is blocked.application/json ProxyError
403The key is valid but may not use this model, route or feature. A guardrail block is a 400, not a 403.application/json ProxyError
404The route or resource does not exist, or this key cannot see it. An unknown model name is a 400 (Invalid model name passed in model=...).application/json ProxyError
408The provider did not answer before the request timed out.application/json ProxyError
413The request body is larger than max_request_size_mb (100 MB by default), or an upload is larger than its own limit. The gateway refused it before reading the key. Docsapplication/json object
422The request body does not have the expected shape, or a budget (key, user, team, organization or end user) is used up: error.type is budget_exceeded. The budget status is spanway_settings.budget_exceeded_status_code, 422 by default. With access requests licensed, a key or team limit refusal also carries error.request_increase, which names the access request to file for a higher limit. Ask from an errorapplication/json ProxyError or HTTPValidationError
429Too many requests. A provider limit or a gateway limit (requests or tokens per minute, or parallel requests) refused the call, and error.type says which. A gateway limit also sends the RateLimit headers and x-spanway-rate-limited-by. Docs With access requests licensed, a key or team limit refusal also carries error.request_increase, which names the access request to file for a higher limit. Ask from an errorapplication/json ProxyError
500The gateway or the provider failed unexpectedly, or the gateway could not reach the provider. Retry, and quote the request id if it keeps failing.application/json ProxyError
502The provider, or a proxy in front of it, answered with something that is not a valid response.application/json ProxyError
503The provider is overloaded or unavailable. With Redis rate limiting, a 503 also means the gateway could not check the rate limit counters and refused the request, because fail_closed_rate_limit_enforcement is on. Docsapplication/json ProxyError

Response fields (200):

FieldTypeRequiredDescription
idstring or nullNoId
resultsarray of object or nullNoResults
metaobject or nullNo

POST /v2/rerank​

Rerank documents by relevance to a query

Score each document against the query and return them ordered by relevance. top_n limits the results and return_documents includes the document text. The gateway serves one request shape for every rerank provider and prices the call per query or per token.

Request body (application/json, object):

FieldTypeRequiredDescription
modelstringYesModel
querystringYesQuery
top_ninteger or nullNoTop N
documentsarray of string or objectYesDocuments
rank_fieldsarray of string or nullNoRank Fields
return_documentsboolean or nullNoReturn Documents
max_chunks_per_docinteger or nullNoMax Chunks Per Doc
max_tokens_per_docinteger or nullNoMax Tokens Per Doc
instructionstring or nullNoInstruction
truncate_prompt_tokensinteger or nullNoTruncate Prompt Tokens
truncation_sideenum: "left", "right" or nullNoTruncation Side
max_tokens_per_queryinteger or nullNoMax Tokens Per Query

Example request:

curl -X POST "http://localhost:4000/v2/rerank" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "my-rerank-model",
"query": "What is the capital of France?",
"documents": [
"Paris is the capital of France.",
"Berlin is the capital of Germany."
],
"top_n": 1
}'

Responses:

StatusDescriptionBody
200The documents ordered by relevance with a score for each.application/json object
400The request is invalid, for example a missing or unsupported parameter, a prompt longer than the model's context window, content a guardrail or the provider refused, or no deployment that satisfies the data policy of the key, team or organization (No deployment of model 'gpt-5.5' satisfies the data policy: ...). error.message says which.application/json ProxyError
401No valid key was sent, or the key has expired or is blocked.application/json ProxyError
403The key is valid but may not use this model, route or feature. A guardrail block is a 400, not a 403.application/json ProxyError
404The route or resource does not exist, or this key cannot see it. An unknown model name is a 400 (Invalid model name passed in model=...).application/json ProxyError
408The provider did not answer before the request timed out.application/json ProxyError
413The request body is larger than max_request_size_mb (100 MB by default), or an upload is larger than its own limit. The gateway refused it before reading the key. Docsapplication/json object
422The request body does not have the expected shape, or a budget (key, user, team, organization or end user) is used up: error.type is budget_exceeded. The budget status is spanway_settings.budget_exceeded_status_code, 422 by default. With access requests licensed, a key or team limit refusal also carries error.request_increase, which names the access request to file for a higher limit. Ask from an errorapplication/json ProxyError or HTTPValidationError
429Too many requests. A provider limit or a gateway limit (requests or tokens per minute, or parallel requests) refused the call, and error.type says which. A gateway limit also sends the RateLimit headers and x-spanway-rate-limited-by. Docs With access requests licensed, a key or team limit refusal also carries error.request_increase, which names the access request to file for a higher limit. Ask from an errorapplication/json ProxyError
500The gateway or the provider failed unexpectedly, or the gateway could not reach the provider. Retry, and quote the request id if it keeps failing.application/json ProxyError
502The provider, or a proxy in front of it, answered with something that is not a valid response.application/json ProxyError
503The provider is overloaded or unavailable. With Redis rate limiting, a 503 also means the gateway could not check the rate limit counters and refused the request, because fail_closed_rate_limit_enforcement is on. Docsapplication/json ProxyError

Response fields (200):

FieldTypeRequiredDescription
idstring or nullNoId
resultsarray of object or nullNoResults
metaobject or nullNo