Repository navigation
Fix EmbeddingModel.embed batching callers with different contexts - #9552
Merged
tim-smart merged 10 commits intoOct 19, 2026
Merged
Conversation
…ller's context Concurrent embed calls that provide different services, such as a provider config override or an HTTP client transform, share one batch that runs with the first caller's context, so every caller gets the first caller's settings.
EmbeddingModel.make now groups embed requests by a key over the caller's context, ignoring per-call services such as the current span, log annotations, and retry metadata. Services are compared by reference, except those passed in the new `settings` option, which are compared with Equal.equals so callers with equal settings still share one embedMany call. The OpenAI and OpenAI-compatible providers pass Config and OpenAiConfig as settings.
🦋 Changeset detectedLatest commit: f73f1f9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 33 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Bun's fetch type requires preconnect, so the root type check failed.
Contributor
Bundle Size AnalysisGenerated from PR build output; treat the content below as untrusted.
|
This branch had an error being deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Type
Description
EmbeddingModel.embedbatches concurrent calls into one providerembedManycall, which runs with the first caller's context. A provider that reads per-call context insideembedManyapplied the first caller's values to every caller. With@effect/ai-openai, two concurrentembedcalls usingwithConfigOverride({ dimensions: 256 })andwithConfigOverride({ dimensions: 1024 })sent one request withdimensions: 256, andwithClientTransformsent one caller's headers with another caller's input.embedManyis not affected.EmbeddingModel.makenow accepts an optionalbatchKey: (context) => unknown. Calls share oneembedManycall only when their keys are equal underEqual, the same contractGraphQLResolverandSqlResolveruse. Providers return the valuesembedManyreads from the caller's context. WithoutbatchKey, all calls share one batch as before.OpenAiEmbeddingModelin@effect/ai-openaiand@effect/ai-openai-compatkeys batches by the caller'sConfigandOpenAiConfigvalues, so equalwithConfigOverridevalues and the samewithClientTransformfunction still share a request.Limitation
Context that
batchKeydoes not return is not isolated. Per-callFetchHttpClient.RequestInit, or a service read by an injected HTTP client, takes the first caller's value for the whole batch. This holds for every batched resolver that goes throughHttpClientand is documented onmakeand on both providers. ARequestResolver-level fix is tracked in #9553.Alternatives considered
embedignores per-call overrides. MakeswithConfigOverridea silent no-op forembed.Testing
packages/ai/openai/test/OpenAiEmbeddingModel.test.tsruns three concurrentembedcalls with independently createdwithConfigOverridevalues: dimensions 2, 3, and 2. It verifies the returned vector dimensions and that the equal overrides batch inputsaandcinto one request whilebgets a separate request. Pre-existing tests are unchanged.f73f1f9f13: all three embedding test files pass (21 tests), independently confirmed in review. Rootpnpm check,pnpm lint-fix, andgit diff --checkalso pass. Toolchain commands ran undernix develop -c.Related
EmbeddingModel.embedruns a batch with the first caller's context #9551RequestResolver:RequestResolver.fromEffectruns every entry with the first caller's context #9553