Skip to content

Fix EmbeddingModel.embed batching callers with different contexts - #9552

Merged
tim-smart merged 10 commits into
Effect-TS:mainfrom
wmaurer:fix/embedding-model-batch-context
Oct 19, 2026
Merged

tim-smart merged 10 commits into
Effect-TS:mainfrom
wmaurer:fix/embedding-model-batch-context

Conversation

@wmaurer

@wmaurer wmaurer commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Type

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

EmbeddingModel.embed batches concurrent calls into one provider embedMany call, which runs with the first caller's context. A provider that reads per-call context inside embedMany applied the first caller's values to every caller. With @effect/ai-openai, two concurrent embed calls using withConfigOverride({ dimensions: 256 }) and withConfigOverride({ dimensions: 1024 }) sent one request with dimensions: 256, and withClientTransform sent one caller's headers with another caller's input. embedMany is not affected.

EmbeddingModel.make now accepts an optional batchKey: (context) => unknown. Calls share one embedMany call only when their keys are equal under Equal, the same contract GraphQLResolver and SqlResolver use. Providers return the values embedMany reads from the caller's context. Without batchKey, all calls share one batch as before.

OpenAiEmbeddingModel in @effect/ai-openai and @effect/ai-openai-compat keys batches by the caller's Config and OpenAiConfig values, so equal withConfigOverride values and the same withClientTransform function still share a request.

Limitation

Context that batchKey does not return is not isolated. Per-call FetchHttpClient.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 through HttpClient and is documented on make and on both providers. A RequestResolver-level fix is tracked in #9553.

Alternatives considered

  • Comparing the caller's whole context, with an ignore list for per-call services such as the current span. Safe by default, but a heuristic that must track core's per-call references, and it stops batching across HTTP requests, which is the main batching case.
  • Fixing config at model construction so embed ignores per-call overrides. Makes withConfigOverride a silent no-op for embed.

Testing

  • One added regression in packages/ai/openai/test/OpenAiEmbeddingModel.test.ts runs three concurrent embed calls with independently created withConfigOverride values: dimensions 2, 3, and 2. It verifies the returned vector dimensions and that the equal overrides batch inputs a and c into one request while b gets a separate request. Pre-existing tests are unchanged.
  • Without the core fix, this regression fails because the dimensions-3 caller receives a two-dimensional vector; restoring the fix makes it pass.
  • Validation at f73f1f9f13: all three embedding test files pass (21 tests), independently confirmed in review. Root pnpm check, pnpm lint-fix, and git diff --check also pass. Toolchain commands ran under nix develop -c.

Related

…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-bot

changeset-bot Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f73f1f9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 33 packages
Name Type
effect Patch
@effect/ai-openai Patch
@effect/ai-openai-compat Patch
@effect/opentelemetry Patch
@effect/vitest Patch
@effect/ai-anthropic Patch
@effect/ai-cloudflare Patch
@effect/ai-openrouter Patch
@effect/ai-typesafe Patch
@effect/atom-react Patch
@effect/atom-solid Patch
@effect/atom-vue Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-deno Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-pglite Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/docgen Patch
@effect/doctest Patch
@effect/graphql-generator Patch
@effect/openapi-generator Patch

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.
@github-actions

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

Generated from PR build output; treat the content below as untrusted.

File Name Current Size Previous Size Difference
arbitrary-combinators.ts 39.11 KB 39.11 KB 0.00 KB (0.00%)
basic.ts 7.02 KB 7.02 KB 0.00 KB (0.00%)
batching.ts 10.87 KB 10.87 KB 0.00 KB (0.00%)
brand.ts 6.71 KB 6.71 KB 0.00 KB (0.00%)
cache.ts 11.33 KB 11.33 KB 0.00 KB (0.00%)
config.ts 22.57 KB 22.57 KB 0.00 KB (0.00%)
differ.ts 21.63 KB 21.63 KB 0.00 KB (0.00%)
http-client.ts 24.01 KB 24.01 KB 0.00 KB (0.00%)
http-router.ts 35.60 KB 35.60 KB 0.00 KB (0.00%)
logger.ts 11.68 KB 11.68 KB 0.00 KB (0.00%)
metric.ts 9.16 KB 9.16 KB 0.00 KB (0.00%)
optic.ts 6.93 KB 6.93 KB 0.00 KB (0.00%)
pubsub.ts 15.77 KB 15.77 KB 0.00 KB (0.00%)
queue.ts 12.59 KB 12.59 KB 0.00 KB (0.00%)
schedule.ts 11.77 KB 11.77 KB 0.00 KB (0.00%)
schema-bigdecimal.ts 13.82 KB 13.82 KB 0.00 KB (0.00%)
schema-binary.ts 40.49 KB 40.49 KB 0.00 KB (0.00%)
schema-class.ts 21.34 KB 21.34 KB 0.00 KB (0.00%)
schema-fromJsonSchemaDocument.ts 32.30 KB 32.30 KB 0.00 KB (0.00%)
schema-representation-roundtrip.ts 27.50 KB 27.50 KB 0.00 KB (0.00%)
schema-string-transformation.ts 15.07 KB 15.07 KB 0.00 KB (0.00%)
schema-string.ts 12.08 KB 12.08 KB 0.00 KB (0.00%)
schema-template-literal.ts 16.00 KB 16.00 KB 0.00 KB (0.00%)
schema-toArbitrary.ts 38.63 KB 38.63 KB 0.00 KB (0.00%)
schema-toCodeDocument.ts 25.69 KB 25.69 KB 0.00 KB (0.00%)
schema-toCodecJson.ts 20.57 KB 20.57 KB 0.00 KB (0.00%)
schema-toEquivalence.ts 20.75 KB 20.75 KB 0.00 KB (0.00%)
schema-toFormatter.ts 20.86 KB 20.86 KB 0.00 KB (0.00%)
schema-toJsonSchemaDocument.ts 25.49 KB 25.49 KB 0.00 KB (0.00%)
schema-toRepresentation.ts 20.85 KB 20.85 KB 0.00 KB (0.00%)
schema.ts 20.53 KB 20.53 KB 0.00 KB (0.00%)
stm.ts 13.87 KB 13.87 KB 0.00 KB (0.00%)
stream.ts 10.12 KB 10.12 KB 0.00 KB (0.00%)

@tim-smart
tim-smart merged commit 44c8c1d into Effect-TS:main Oct 19, 2026
9 of 10 checks passed

This branch had an error being deployed

1 failed deployment
fork — f73f1f9f Deployed Oct 19, 2026 by effect-bot via approval-gate #21355
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EmbeddingModel.embed runs a batch with the first caller's context

3 participants