Cloudflare - v1.3.0
    Preparing search index...

    Class AiSearchBinding<E>

    Accessor for a single-instance AI Search binding.

    AI Search is managed retrieval: Cloudflare indexes a source, and a request asks it a question in words rather than in vectors. That is the difference from VectorizeBinding, which this package has had since 1.0.0 — with Vectorize a Worker owns the embedding model, the chunking, and the index lifecycle; here it owns none of them. Both are worth having, and which one is right turns on whether the embedding pipeline is something you want to run.

    The older path to the same product hung off the Workers AI binding, as env.AI.autorag(id) and env.AI.aiSearch(). Both are deprecated in the @cloudflare/workers-types version this package pins, in favour of the standalone bindings this class and AiSearchNamespaceBinding wrap. So although AIBinding already exists, routing AI Search through it would have adopted a deprecated surface on the day it was written.

    The kernel names no retrieval capability, and the same reasoning applies as for WorkflowBinding: an interface with one implementation and no consumers is ceremony. Managed retrieval is a plausible candidate later — "ask a corpus a question" is not Cloudflare vocabulary — but the seam should be drawn by a second implementation, not guessed at ahead of one.

    const filings = new AiSearchBinding()

    const { chunks } = await filings.search(env, { query: 'dividend policy changes in 2026' })

    Bayu Dwiyan Satria

    1.0.0

    1.3.0

    Type Parameters

    Hierarchy (View Summary)

    • Binding<AiSearchInstance, E>
      • AiSearchBinding
    Index
    settings: () => AiSearchSettings

    This accessor's settings, resolved on first read.

    • get name(): string

      The binding name this instance resolves.

      Returns string

      The binding name as declared in wrangler.json.

    • Answers a conversation, grounded in the indexed corpus.

      Parameters

      • env: E

        The Worker environment.

      • params: AiSearchChatCompletionsRequest

        The chat request.

      Returns Promise<AiSearchChatCompletionsResponse>

      The generated answer, plus the chunks it was grounded in.

      Retrieval and generation in one call. The response carries the chunks the answer was drawn from alongside the answer itself, which is what makes a citation possible — return them, or a reader has no way to check the model.

      MissingBindingError When the AI Search binding is absent.

    • Answers a conversation as a stream of server-sent events.

      Parameters

      • env: E

        The Worker environment.

      • params: AiSearchChatCompletionsRequest

        The chat request.

      Returns Promise<ReadableStream<any>>

      The event stream, ready to return as a response body.

      Separate from chat rather than a stream: true flag on it, because the two return different things — a parsed response and a ReadableStream — and a boolean argument that changes a return type is a discriminated union pretending to be an option.

      MissingBindingError When the AI Search binding is absent.

    • Reads the instance's metadata.

      Parameters

      • env: E

        The Worker environment.

      Returns Promise<AiSearchInstanceInfo>

      What this instance is and how it is configured.

      MissingBindingError When the AI Search binding is absent.

    • Checks whether the binding is available on the given environment.

      Use this to degrade gracefully when a binding is optional.

      Parameters

      • env: E

        The Worker environment.

      Returns boolean

      true when the binding is present.

    • Resolves the binding, failing fast when it is not configured.

      Parameters

      • env: E

        The Worker environment.

      Returns AiSearchInstance

      The resolved binding.

      MissingBindingError When the binding is absent from the environment.

    • Retrieves the chunks matching a query.

      Parameters

      • env: E

        The Worker environment.

      • params: AiSearchSearchRequest

        The search request, as a query or a conversation.

      Returns Promise<AiSearchSearchResponse>

      The matching chunks, each with its score and source item.

      Retrieval only — this returns passages and their scores, and generates no prose. Use it when the Worker does its own reasoning over the results, and chat when it wants an answer written.

      The request takes either a query string or a messages conversation, never both; the platform's own types enforce that.

      MissingBindingError When the AI Search binding is absent.

    • Reads the instance's indexing statistics.

      Parameters

      • env: E

        The Worker environment.

      Returns Promise<AiSearchStatsResponse>

      Counts per status, last activity, and engine details.

      Item counts by status and the last activity time — the answer to "is the corpus behind?", which is the question a search over stale content raises and a search response cannot answer.

      MissingBindingError When the AI Search binding is absent.

    • Resolves the binding without throwing.

      Parameters

      • env: E

        The Worker environment.

      Returns AiSearchInstance

      The resolved binding, or null when it is not configured.

    • Applies the configured retrieval options to a request that names none.

      Type Parameters

      • T extends { ai_search_options?: AiSearchOptions }

        The request shape, either a search or a chat request.

      Parameters

      • params: T

        The caller-supplied request.

      Returns T

      The request with configured options filled in.

      All or nothing, for the reason AiSearchSettings.options gives: a request naming ai_search_options owns the whole object, because merging four nested sub-objects field by field would give a call site an effective configuration it could not predict from reading itself.