Skip to content
safran

Type to search. Enter opens the first result.

    OpenAI-compatible embeddings

    The openai-compatible embedder calls any server that implements the OpenAI /embeddings endpoint: a local embedding server, an inference server in your own network, or a hosted API. Use it when you do not run Ollama, or when you want a model Ollama does not serve.

    Profile entry

    A local server without authentication:

    type: openai-compatible
    url: http://localhost:8080/v1
    model: bge-m3

    An API that needs a key:

    type: openai-compatible
    url: https://embeddings.acme.test/v1
    model: acme-embed-v2
    token_env: EMBEDDINGS_API_KEY
    dimensions: 1024

    url is the base URL up to and including the version segment; the connector appends /embeddings. Then:

    safran doctor
    safran sync
    safran search "kısmi iade"
    search.ts
    import { createSafran, isSafranError } from "@safran/core";
    
    await using safran = await createSafran({ profile: "safran.yaml" });
    
    try {
      const { hits } = await safran.code.search("kısmi iade", { limit: 5 });
      for (const hit of hits) console.log(hit.repo, hit.path, hit.startLine, hit.channels);
    } catch (error) {
      // The index was built with another embedder: fall back to words until the next sync.
      if (!isSafranError(error, "INDEX_NOT_READY")) throw error;
      const { hits } = await safran.code.search("iade", { mode: "lexical", limit: 5 });
      for (const hit of hits) console.log(hit.repo, hit.path, hit.startLine);
    }

    Authentication

    token_env is optional. When set, the key is sent as Authorization: Bearer, and the variable must hold a value: a missing one fails the first embedding call with AUTH_MISSING, not the start of the program. When the field is left out, no credentials are sent, which suits a local server.

    The key is only sent to the origin of url. A redirect to another origin is not followed.

    Configuration reference

    FieldTypeDefaultRequiredMeaning
    type"openai-compatible"yesSelects this plugin.
    urlURLyesBase URL up to and including the version segment, e.g. http://localhost:8080/v1. No credentials, query string or fragment.
    modelstringyesModel name sent with every request. Part of the embedder id.
    token_envvariable namenoVariable holding the API key. Omit for local servers without auth.
    batch_sizeinteger, 1 to 204864noInputs per request.
    timeout_msinteger, at least 100060000noTimeout of one attempt, in milliseconds.
    dimensionsinteger, at least 1noRequested vector size, for models that support shortening. Part of the embedder id.

    The entry has no id: a profile has exactly one embedder.

    How it calls the server

    • Endpoint. POST <url>/embeddings with model, input (a list of strings) and, when set, dimensions.
    • Batching. Inputs are sent batch_size at a time, one request after the other.
    • Timeout and retries. Each attempt may take timeout_ms. Network failures and retryable statuses (408, 425, 429, 500, 502, 503, 504) are retried up to DEFAULT_HTTP_MAX_RETRIES (3) times, waiting for Retry-After when the server sends it. After that, 401 and 403 are AUTH_FAILED, 404 is NOT_FOUND, 429 is RATE_LIMITED, the rest UPSTREAM.
    • Response. The answer must have a data array with one row per input. Rows are put in order by their index when every row carries a distinct, valid one; otherwise the order of arrival is kept, because sorting by incomplete indexes would pair vectors with the wrong texts. Every vector is checked (same size, finite numbers) and scaled to unit length.

    A response without data is UPSTREAM, with a hint to check that url points at the API root, usually ending in /v1.

    Inputs the model refuses as too long

    These servers answer 400 ("maximum context length is …") or 413 when one input does not fit the model, and refuse the whole batch with it. The handling is the same as for Ollama:

    1. If a batch is refused for length, its inputs are embedded one by one.
    2. An input refused on its own is shortened to DEFAULT_SHORTEN_FACTOR (75 %) of its text and tried again, up to DEFAULT_MAX_SHORTEN_ATTEMPTS (6) times, with a warning in the log when a shortened version was embedded.
    3. One that never fits fails with INVALID_INPUT and details.reason set to input_too_long. The code index then stores that one chunk without a vector (lexical search and grep still find it) and counts it under filesSkipped.chunk_too_long_for_embedder.

    A refusal is recognised by status 413, or by a 400, 422 or 500 whose error text mentions the context or token limit. A server that words it in a way the connector does not recognise fails the batch with UPSTREAM; choose a model with a larger context then.

    The embedder id, and changing the model

    The id is openai-compatible:<model>, with @<dimensions> appended when dimensions is set: openai-compatible:acme-embed-v2@1024. The index records the id its vectors were made with, and vectors from different ids never mix.

    • Changing model or dimensions changes the id. Vector search then fails with INDEX_NOT_READY until safran sync has re-embedded the repositories; lexical search, grep and reading files keep working. The exact behaviour and messages are on the Ollama page and are the same here.
    • Switching between embedder types changes the id even for the same model: ollama:bge-m3 and openai-compatible:bge-m3 are different ids, and the index is rebuilt.
    • The id does not include url. Pointing the entry at another server that offers a model under the same name keeps the id, and the stored vectors are used as they are. If that server's model is in fact a different one, search results are wrong without an error, unless the vector size differs, which is caught (INDEX_NOT_READY). When you move servers and are not certain the model is identical, delete the index directory and run safran sync.

    Limits

    ConstantValueMeaning
    DEFAULT_OPENAI_COMPATIBLE_BATCH_SIZE / MAX_OPENAI_COMPATIBLE_BATCH_SIZE64 / 2048Default and largest batch_size.
    DEFAULT_OPENAI_COMPATIBLE_TIMEOUT_MS60000Default timeout_ms.
    MIN_EMBEDDER_TIMEOUT_MS1000Smallest timeout_ms a profile may set.
    DEFAULT_HTTP_MAX_RETRIES3Retries after the first attempt.
    DEFAULT_SHORTEN_FACTOR / DEFAULT_MAX_SHORTEN_ATTEMPTS0.75 / 6Shortening of inputs the server refuses for length.

    Your server may accept fewer inputs per request than batch_size; lower it if requests are rejected.

    Known caveats

    Nothing in this connector is marked UNVERIFIED, and nothing in the source says it was run against a particular server: it is tested with fixtures shaped like the OpenAI embeddings response. What to keep in mind:

    • "Compatible" servers differ in details. The connector relies on three things only: POST /embeddings, a data array, and an embedding list of numbers in each row.
    • dimensions is passed through. A server that does not support it may ignore it or reject the request.
    • The kind hint of the embedder contract (query or document) is not used: queries and chunks are sent the same way, so models that expect different prefixes for the two are not served by this connector as it is. A custom embedder plugin can add them.

    See also

    • Ollama, the default embedder, where nothing leaves the machine.
    • Evaluation, for comparing models on your own code before switching.
    • Connectors overview for the rules shared by all connectors.