Skip to content
Start here

Multi-Instance Chat Completions

aisearch.namespaces.chat_completions(strname, NamespaceChatCompletionsParams**kwargs) -> NamespaceChatCompletionsResponse
POST/accounts/{account_id}/ai-search/namespaces/{name}/chat/completions

Performs a chat completion request against multiple AI Search instances in parallel, merging retrieved content as context for generating a response.

Security
API Token

The preferred authorization scheme for interacting with the Cloudflare API. Create a token.

Example:Authorization: Bearer Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY
API Email + API Key

The previous authorization scheme for interacting with the Cloudflare API, used in conjunction with a Global API key.

Example:X-Auth-Email: user@example.com

The previous authorization scheme for interacting with the Cloudflare API. When possible, use API tokens instead of Global API keys.

Example:X-Auth-Key: 144c9defac04969c7bfad8efaa8ea194
ParametersExpand Collapse
account_id: str
name: str
aisearch_options: AISearchOptions
instance_ids: Sequence[str]
cache: Optional[AISearchOptionsCache]
cache_threshold: Optional[Literal["super_strict_match", "close_enough", "flexible_friend", "anything_goes"]]
One of the following:
"super_strict_match"
"close_enough"
"flexible_friend"
"anything_goes"
enabled: Optional[bool]
custom_metadata: Optional[Dict[str, Union[str, float, bool]]]

Metadata added to AI Gateway logs for requests this operation triggers. Accepts up to 2 string, number, or boolean entries. Reserved keys: ‘ai-search’, ‘task’, ‘origin’, and any key beginning with ‘cf.’.

One of the following:
str
float
bool
query_rewrite: Optional[AISearchOptionsQueryRewrite]
enabled: Optional[bool]
model: Optional[str]

A Workers AI model ID or an AI Gateway model ID compatible with the OpenAI Chat Completions API. An empty string uses the configured or default model.

rewrite_prompt: Optional[str]
reranking: Optional[AISearchOptionsReranking]
enabled: Optional[bool]
match_threshold: Optional[float]
maximum1
minimum0
model: Optional[str]
retrieval: Optional[AISearchOptionsRetrieval]
boost_by: Optional[Iterable[AISearchOptionsRetrievalBoostBy]]

Metadata fields to boost search results by. Overrides the instance-level boost_by config. Direction defaults to ‘asc’ for numeric/datetime fields, ‘exists’ for text/boolean fields. Fields must match ‘timestamp’ or a defined custom_metadata field.

field: str

Metadata field name to boost by. Use ‘timestamp’ for document freshness, or any custom_metadata field. Numeric and datetime fields support all four directions (asc, desc, exists, not_exists); text/boolean fields only support exists/not_exists.

maxLength64
minLength1
direction: Optional[Literal["asc", "desc", "exists", "not_exists"]]

Boost direction. ‘desc’ = higher values rank higher (e.g. newer timestamps). ‘asc’ = lower values rank higher. ‘exists’ = boost chunks that have the field. ‘not_exists’ = boost chunks that lack the field. Optional — defaults to ‘asc’ for numeric/datetime fields, ‘exists’ for text/boolean fields.

One of the following:
"asc"
"desc"
"exists"
"not_exists"
context_expansion: Optional[int]
maximum3
minimum0
filters: Optional[Dict[str, object]]
fusion_method: Optional[Literal["max", "rrf"]]
One of the following:
"max"
"rrf"
keyword_match_mode: Optional[Literal["and", "or"]]

Controls which documents become candidates for BM25 scoring. ‘and’ restricts candidates to documents containing all query terms; ‘or’ includes any document containing at least one term and ranks results according to BM25 relevance. When omitted, falls back to the instance-level retrieval_options.keyword_match_mode, then to ‘and’.

One of the following:
"and"
"or"
match_threshold: Optional[float]
maximum1
minimum0
max_num_results: Optional[int]
maximum50
minimum1
retrieval_type: Optional[Literal["vector", "keyword", "hybrid"]]
One of the following:
"vector"
"keyword"
"hybrid"
return_on_failure: Optional[bool]
messages: Iterable[Message]
content: Union[str, Iterable[MessageContentUnionMember1], Optional[str]]
One of the following:
str
Iterable[MessageContentUnionMember1]
One of the following:
class MessageContentUnionMember1UnionMember0: …
text: str
minLength1
type: Literal["text"]
class MessageContentUnionMember1UnionMember1: …
image_url: MessageContentUnionMember1UnionMember1ImageURL
url: str
maxLength20971520
minLength1
type: Literal["image_url"]
class MessageContentUnionMember1UnionMember2: …
file: MessageContentUnionMember1UnionMember2File
filename: str
maxLength255
minLength1
file_data: Optional[str]
maxLength13981144
minLength1
file_id: Optional[str]
type: Literal["file"]
Optional[str]
role: Literal["system", "developer", "user", 2 more]
One of the following:
"system"
"developer"
"user"
"assistant"
"tool"
model: Optional[str]

A Workers AI model ID or an AI Gateway model ID compatible with the OpenAI Chat Completions API. An empty string uses the configured or default model.

stream: Optional[bool]
ReturnsExpand Collapse
class NamespaceChatCompletionsResponse: …
choices: List[Choice]
message: ChoiceMessage
content: Union[str, List[ChoiceMessageContentUnionMember1], Optional[str]]
One of the following:
str
List[ChoiceMessageContentUnionMember1]
One of the following:
class ChoiceMessageContentUnionMember1UnionMember0: …
text: str
minLength1
type: Literal["text"]
class ChoiceMessageContentUnionMember1UnionMember1: …
image_url: ChoiceMessageContentUnionMember1UnionMember1ImageURL
url: str
maxLength20971520
minLength1
type: Literal["image_url"]
class ChoiceMessageContentUnionMember1UnionMember2: …
file: ChoiceMessageContentUnionMember1UnionMember2File
filename: str
maxLength255
minLength1
file_data: Optional[str]
maxLength13981144
minLength1
file_id: Optional[str]
type: Literal["file"]
Optional[str]
role: Literal["system", "developer", "user", 2 more]
One of the following:
"system"
"developer"
"user"
"assistant"
"tool"
index: Optional[int]
chunks: List[Chunk]
id: str
instance_id: str
score: float
maximum1
minimum0
text: str
type: str
item: Optional[ChunkItem]
key: str
metadata: Optional[Dict[str, object]]
timestamp: Optional[float]
media_end_ms: Optional[int]

Audio/video transcript chunks only: end, in milliseconds from the start of the media (whole seconds), of the last transcript section the matched chunk covers. Describes the matched chunk before any context expansion, not the expanded text.

minimum0
media_start_ms: Optional[int]

Audio/video transcript chunks only: start, in milliseconds from the start of the media (whole seconds), of the transcript section the matched chunk begins in. Describes the matched chunk before any context expansion, not the expanded text.

minimum0
scoring_details: Optional[ChunkScoringDetails]
fusion_method: Optional[Literal["rrf", "max"]]
One of the following:
"rrf"
"max"
keyword_rank: Optional[float]
keyword_score: Optional[float]
minimum0
reranking_score: Optional[float]
maximum1
minimum0
vector_rank: Optional[float]
vector_score: Optional[float]
maximum1
minimum0
source_media_type: Optional[Literal["audio", "video"]]

Audio/video transcript chunks only: type of the source media file. Omitted for all other chunks.

One of the following:
"audio"
"video"
id: Optional[str]
errors: Optional[List[Error]]
instance_id: str
message: str
model: Optional[str]
object: Optional[str]

Multi-Instance Chat Completions

import os
from cloudflare import Cloudflare

client = Cloudflare(
    api_token=os.environ.get("CLOUDFLARE_API_TOKEN"),  # This is the default and can be omitted
)
response = client.aisearch.namespaces.chat_completions(
    name="my-namespace",
    account_id="c3dc5f0b34a14ff8e1b3ec04895e1b22",
    aisearch_options={
        "instance_ids": ["my-ai-search"]
    },
    messages=[{
        "content": "string",
        "role": "system",
    }],
)
print(response.id)
{
  "choices": [
    {
      "message": {
        "content": "string",
        "role": "system"
      },
      "index": 0
    }
  ],
  "chunks": [
    {
      "id": "id",
      "instance_id": "instance_id",
      "score": 0,
      "text": "text",
      "type": "type",
      "item": {
        "key": "key",
        "metadata": {
          "foo": "bar"
        },
        "timestamp": 0
      },
      "media_end_ms": 0,
      "media_start_ms": 0,
      "scoring_details": {
        "fusion_method": "rrf",
        "keyword_rank": 0,
        "keyword_score": 0,
        "reranking_score": 0,
        "vector_rank": 0,
        "vector_score": 0
      },
      "source_media_type": "audio"
    }
  ],
  "id": "id",
  "errors": [
    {
      "instance_id": "instance_id",
      "message": "message"
    }
  ],
  "model": "model",
  "object": "object"
}
Returns Examples
{
  "choices": [
    {
      "message": {
        "content": "string",
        "role": "system"
      },
      "index": 0
    }
  ],
  "chunks": [
    {
      "id": "id",
      "instance_id": "instance_id",
      "score": 0,
      "text": "text",
      "type": "type",
      "item": {
        "key": "key",
        "metadata": {
          "foo": "bar"
        },
        "timestamp": 0
      },
      "media_end_ms": 0,
      "media_start_ms": 0,
      "scoring_details": {
        "fusion_method": "rrf",
        "keyword_rank": 0,
        "keyword_score": 0,
        "reranking_score": 0,
        "vector_rank": 0,
        "vector_score": 0
      },
      "source_media_type": "audio"
    }
  ],
  "id": "id",
  "errors": [
    {
      "instance_id": "instance_id",
      "message": "message"
    }
  ],
  "model": "model",
  "object": "object"
}