fix(gemini): split native embeddings into batches of 100

PC
2d ago
+94-19

Native Gemini embeddings (the default api_mode) sent every OpenAI input in a single batchEmbedContents call. That endpoint accepts at most 100 requests per batch, so any /v1/embeddings request with more than 100 inputs failed upstream with 400 INVALID_ARGUMENT: at most 100 requests can be in one batch, while the same request works against OpenAI (which accepts up to 2048 inputs).

Root cause: nativeEmbeddings in internal/providers/gemini/embeddings.go built one batchEmbedContents body for all inputs.

Fix: split the per-input requests into chunks of 100 (maxNativeEmbedBatchSize), call batchEmbedContents once per chunk, concatenate the embeddings in order so index keeps matching the input position, and sum usageMetadata across chunks. The per-call count check stays, now per chunk. Requests with ≤100 inputs still make exactly one call.

Test: TestNativeEmbeddings_SplitsInputsIntoBatchesOf100 sends 150 inputs to a fake server that rejects batches over 100 like the real API; it checks the calls are 100 + 50, that the 150 embeddings come back in input order with matching indexes, and that usage is summed. It fails on main with the upstream 400 and passes with this change.

Summary by CodeRabbit

  • New Features

    • Large text-embedding requests are now handled in batches, allowing more inputs to be processed in a single request while preserving result order.
    • Usage totals are combined across batches.
  • Bug Fixes

    • Requests now return a clear provider error if a batch produces a different number of embeddings than inputs.