@mastra/rag 2.1.3 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,85 @@
1
1
  # @mastra/rag
2
2
 
3
+ ## 2.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Add RAG observability (#10898) ([#15137](https://github.com/mastra-ai/mastra/pull/15137))
8
+
9
+ Surfaces RAG ingestion and query operations in Mastra's AI tracing.
10
+
11
+ New span types in `@mastra/core/observability`:
12
+ - `RAG_INGESTION` (root) — wraps an ingestion pipeline run
13
+ - `RAG_EMBEDDING` — embedding call (used by ingestion and query)
14
+ - `RAG_VECTOR_OPERATION` — vector store I/O (`query`/`upsert`/`delete`/`fetch`)
15
+ - `RAG_ACTION` — `chunk` / `extract_metadata` / `rerank`
16
+ - `GRAPH_ACTION` — non-RAG graph `build` / `traverse` / `update` / `prune`
17
+
18
+ New helpers exported from `@mastra/core/observability`:
19
+ - `startRagIngestion(opts)` — manual: returns `{ span, observabilityContext }`
20
+ - `withRagIngestion(opts, fn)` — scoped: runs `fn(observabilityContext)`,
21
+ attaches the return value as the span's output, routes thrown errors to
22
+ `span.error(...)`
23
+
24
+ Wired in `@mastra/rag`:
25
+ - `vectorQuerySearch` emits `RAG_EMBEDDING` (mode: `query`) and
26
+ `RAG_VECTOR_OPERATION` (operation: `query`)
27
+ - `rerank` / `rerankWithScorer` emit `RAG_ACTION` (action: `rerank`)
28
+ - `MDocument.chunk` emits `RAG_ACTION` (action: `chunk`) and
29
+ `RAG_ACTION` (action: `extract_metadata`)
30
+ - `createGraphRAGTool` emits `GRAPH_ACTION` (action: `build` / `traverse`)
31
+ - `createVectorQueryTool` and `createGraphRAGTool` thread
32
+ `observabilityContext` from the agent's `TOOL_CALL` span automatically
33
+
34
+ All new instrumentation is opt-in: functions accept an optional
35
+ `observabilityContext` and no-op when absent, so existing callers are
36
+ unaffected.
37
+
38
+ ### Patch Changes
39
+
40
+ - Updated dependencies [[`8db7663`](https://github.com/mastra-ai/mastra/commit/8db7663c9a9c735828094c359d2e327fd4f8fba3), [`153e864`](https://github.com/mastra-ai/mastra/commit/153e86476b425db7cd0dc8490050096e92964a38), [`715710d`](https://github.com/mastra-ai/mastra/commit/715710d12fa47cf88e09d41f13843eddc29327b0), [`378c6c4`](https://github.com/mastra-ai/mastra/commit/378c6c4755726e8d8cf83a14809b350b90d46c62), [`9f91fd5`](https://github.com/mastra-ai/mastra/commit/9f91fd538ab2a44f8cc740bcad8e51205f74fbea), [`ba6fa9c`](https://github.com/mastra-ai/mastra/commit/ba6fa9cc0f3e1912c49fd70d4c3bb8c44903ddaa)]:
41
+ - @mastra/core@1.24.0
42
+
43
+ ## 2.2.0-alpha.0
44
+
45
+ ### Minor Changes
46
+
47
+ - Add RAG observability (#10898) ([#15137](https://github.com/mastra-ai/mastra/pull/15137))
48
+
49
+ Surfaces RAG ingestion and query operations in Mastra's AI tracing.
50
+
51
+ New span types in `@mastra/core/observability`:
52
+ - `RAG_INGESTION` (root) — wraps an ingestion pipeline run
53
+ - `RAG_EMBEDDING` — embedding call (used by ingestion and query)
54
+ - `RAG_VECTOR_OPERATION` — vector store I/O (`query`/`upsert`/`delete`/`fetch`)
55
+ - `RAG_ACTION` — `chunk` / `extract_metadata` / `rerank`
56
+ - `GRAPH_ACTION` — non-RAG graph `build` / `traverse` / `update` / `prune`
57
+
58
+ New helpers exported from `@mastra/core/observability`:
59
+ - `startRagIngestion(opts)` — manual: returns `{ span, observabilityContext }`
60
+ - `withRagIngestion(opts, fn)` — scoped: runs `fn(observabilityContext)`,
61
+ attaches the return value as the span's output, routes thrown errors to
62
+ `span.error(...)`
63
+
64
+ Wired in `@mastra/rag`:
65
+ - `vectorQuerySearch` emits `RAG_EMBEDDING` (mode: `query`) and
66
+ `RAG_VECTOR_OPERATION` (operation: `query`)
67
+ - `rerank` / `rerankWithScorer` emit `RAG_ACTION` (action: `rerank`)
68
+ - `MDocument.chunk` emits `RAG_ACTION` (action: `chunk`) and
69
+ `RAG_ACTION` (action: `extract_metadata`)
70
+ - `createGraphRAGTool` emits `GRAPH_ACTION` (action: `build` / `traverse`)
71
+ - `createVectorQueryTool` and `createGraphRAGTool` thread
72
+ `observabilityContext` from the agent's `TOOL_CALL` span automatically
73
+
74
+ All new instrumentation is opt-in: functions accept an optional
75
+ `observabilityContext` and no-op when absent, so existing callers are
76
+ unaffected.
77
+
78
+ ### Patch Changes
79
+
80
+ - Updated dependencies [[`8db7663`](https://github.com/mastra-ai/mastra/commit/8db7663c9a9c735828094c359d2e327fd4f8fba3), [`715710d`](https://github.com/mastra-ai/mastra/commit/715710d12fa47cf88e09d41f13843eddc29327b0), [`378c6c4`](https://github.com/mastra-ai/mastra/commit/378c6c4755726e8d8cf83a14809b350b90d46c62), [`9f91fd5`](https://github.com/mastra-ai/mastra/commit/9f91fd538ab2a44f8cc740bcad8e51205f74fbea), [`ba6fa9c`](https://github.com/mastra-ai/mastra/commit/ba6fa9cc0f3e1912c49fd70d4c3bb8c44903ddaa)]:
81
+ - @mastra/core@1.24.0-alpha.1
82
+
3
83
  ## 2.1.3
4
84
 
5
85
  ### Patch Changes
@@ -3,7 +3,7 @@ name: mastra-rag
3
3
  description: Documentation for @mastra/rag. Use when working with @mastra/rag APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/rag"
6
- version: "2.1.3"
6
+ version: "2.2.0"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.1.3",
2
+ "version": "2.2.0",
3
3
  "package": "@mastra/rag",
4
4
  "exports": {},
5
5
  "modules": {}
@@ -1,3 +1,4 @@
1
+ import type { ObservabilityContext } from '@mastra/core/observability';
1
2
  import { Document as Chunk } from './schema/index.js';
2
3
  import type { ChunkParams, ExtractParams, HTMLChunkOptions, RecursiveChunkOptions, CharacterChunkOptions, TokenChunkOptions, MarkdownChunkOptions, SemanticMarkdownChunkOptions, JsonChunkOptions, LatexChunkOptions, SentenceChunkOptions } from './types.js';
3
4
  export declare class MDocument {
@@ -28,7 +29,9 @@ export declare class MDocument {
28
29
  chunkMarkdown(options?: MarkdownChunkOptions): Promise<void>;
29
30
  chunkSentence(options?: SentenceChunkOptions): Promise<void>;
30
31
  chunkSemanticMarkdown(options?: SemanticMarkdownChunkOptions): Promise<void>;
31
- chunk(params?: ChunkParams): Promise<Chunk[]>;
32
+ chunk(params?: ChunkParams, options?: {
33
+ observabilityContext?: ObservabilityContext;
34
+ }): Promise<Chunk[]>;
32
35
  getDocs(): Chunk[];
33
36
  getText(): string[];
34
37
  getMetadata(): Record<string, any>[];
@@ -1 +1 @@
1
- {"version":3,"file":"document.d.ts","sourceRoot":"","sources":["../../src/document/document.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,QAAQ,IAAI,KAAK,EAAgC,MAAM,UAAU,CAAC;AAU3E,OAAO,KAAK,EACV,WAAW,EAEX,aAAa,EACb,gBAAgB,EAChB,qBAAqB,EACrB,qBAAqB,EACrB,iBAAiB,EACjB,oBAAoB,EACpB,4BAA4B,EAC5B,gBAAgB,EAChB,iBAAiB,EACjB,oBAAoB,EAErB,MAAM,SAAS,CAAC;AAGjB,qBAAa,SAAS;IACpB,OAAO,CAAC,MAAM,CAAU;IACxB,OAAO,CAAC,IAAI,CAAS;gBAET,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE;QAAE,IAAI,EAAE;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;SAAE,EAAE,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE;IAOhG,eAAe,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,aAAa,GAAG,OAAO,CAAC,SAAS,CAAC;IAuDzG,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ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,IAAI,KAAK,EAAE;IAIlB,OAAO,IAAI,MAAM,EAAE;IAInB,WAAW,IAAI,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE;CAGrC"}
package/dist/index.cjs CHANGED
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ var observability = require('@mastra/core/observability');
3
4
  var agent = require('@mastra/core/agent');
4
5
  var crypto = require('crypto');
5
6
  var zod = require('zod');
@@ -6917,15 +6918,47 @@ var MDocument = class _MDocument {
6917
6918
  const textSplit = rt.transformDocuments(this.chunks);
6918
6919
  this.chunks = textSplit;
6919
6920
  }
6920
- async chunk(params) {
6921
+ async chunk(params, options) {
6921
6922
  const { strategy: passedStrategy, extract, ...chunkOptions } = params || {};
6922
6923
  const strategy = passedStrategy || this.defaultStrategy();
6923
- validateChunkParams(strategy, chunkOptions);
6924
- await this.chunkBy(strategy, chunkOptions);
6925
- if (extract) {
6926
- await this.extractMetadata(extract);
6924
+ const parentSpan = options?.observabilityContext?.tracingContext?.currentSpan;
6925
+ const chunkSpan = parentSpan?.createChildSpan({
6926
+ type: observability.SpanType.RAG_ACTION,
6927
+ name: `rag chunk: ${strategy}`,
6928
+ input: { strategy },
6929
+ attributes: {
6930
+ action: "chunk",
6931
+ strategy,
6932
+ chunkSize: chunkOptions?.size,
6933
+ chunkOverlap: chunkOptions?.overlap
6934
+ }
6935
+ });
6936
+ try {
6937
+ validateChunkParams(strategy, chunkOptions);
6938
+ await this.chunkBy(strategy, chunkOptions);
6939
+ if (extract) {
6940
+ const extractSpan = chunkSpan?.createChildSpan({
6941
+ type: observability.SpanType.RAG_ACTION,
6942
+ name: "rag extract metadata",
6943
+ attributes: {
6944
+ action: "extract_metadata",
6945
+ extractor: Object.keys(extract).join(",")
6946
+ }
6947
+ });
6948
+ try {
6949
+ await this.extractMetadata(extract);
6950
+ } catch (err) {
6951
+ extractSpan?.error({ error: err, endSpan: true });
6952
+ throw err;
6953
+ }
6954
+ extractSpan?.end();
6955
+ }
6956
+ chunkSpan?.end({ output: { chunkCount: this.chunks.length } });
6957
+ return this.chunks;
6958
+ } catch (err) {
6959
+ chunkSpan?.error({ error: err, endSpan: true });
6960
+ throw err;
6927
6961
  }
6928
- return this.chunks;
6929
6962
  }
6930
6963
  getDocs() {
6931
6964
  return this.chunks;
@@ -7047,14 +7080,27 @@ async function executeRerank({
7047
7080
  scorer,
7048
7081
  options
7049
7082
  }) {
7050
- const { queryEmbedding, topK = 3 } = options;
7083
+ const { queryEmbedding, topK = 3, observabilityContext } = options;
7084
+ const rerankSpan = observabilityContext?.tracingContext?.currentSpan?.createChildSpan({
7085
+ type: observability.SpanType.RAG_ACTION,
7086
+ name: `rag rerank`,
7087
+ input: { query, candidateCount: results.length },
7088
+ attributes: {
7089
+ action: "rerank",
7090
+ candidateCount: results.length,
7091
+ topN: topK,
7092
+ scorer: scorer?.constructor?.name
7093
+ }
7094
+ });
7051
7095
  const weights = {
7052
7096
  ...DEFAULT_WEIGHTS,
7053
7097
  ...options.weights
7054
7098
  };
7055
7099
  const sum = Object.values(weights).reduce((acc, w) => acc.plus(w.toString()), new big_js.Big(0));
7056
7100
  if (!sum.eq(1)) {
7057
- throw new Error(`Weights must add up to 1. Got ${sum} from ${weights}`);
7101
+ const err = new Error(`Weights must add up to 1. Got ${sum} from ${weights}`);
7102
+ rerankSpan?.error({ error: err, endSpan: true });
7103
+ throw err;
7058
7104
  }
7059
7105
  const resultLength = results.length;
7060
7106
  const queryAnalysis = queryEmbedding ? analyzeQueryEmbedding(queryEmbedding) : null;
@@ -7087,7 +7133,9 @@ async function executeRerank({
7087
7133
  };
7088
7134
  })
7089
7135
  );
7090
- return scoredResults.sort((a, b) => b.score - a.score).slice(0, topK);
7136
+ const final = scoredResults.sort((a, b) => b.score - a.score).slice(0, topK);
7137
+ rerankSpan?.end({ output: { returned: final.length } });
7138
+ return final;
7091
7139
  }
7092
7140
  async function rerankWithScorer({
7093
7141
  results,
@@ -7377,33 +7425,67 @@ var vectorQuerySearch = async ({
7377
7425
  includeVectors = false,
7378
7426
  maxRetries = 2,
7379
7427
  databaseConfig = {},
7380
- providerOptions
7428
+ providerOptions,
7429
+ observabilityContext
7381
7430
  }) => {
7431
+ const parentSpan = observabilityContext?.tracingContext?.currentSpan;
7432
+ const embedSpan = parentSpan?.createChildSpan({
7433
+ type: observability.SpanType.RAG_EMBEDDING,
7434
+ name: `rag embed: query`,
7435
+ input: queryText,
7436
+ attributes: {
7437
+ mode: "query",
7438
+ model: model?.modelId,
7439
+ provider: model?.provider,
7440
+ inputCount: 1
7441
+ }
7442
+ });
7382
7443
  let embeddingResult;
7383
- if (model.specificationVersion === "v3") {
7384
- embeddingResult = await vector.embedV3({
7385
- model,
7386
- value: queryText,
7387
- maxRetries,
7388
- // Type assertion needed: providerOptions type is a union, but embedV3 expects specific version
7389
- ...providerOptions && { providerOptions }
7390
- });
7391
- } else if (model.specificationVersion === "v2") {
7392
- embeddingResult = await vector.embedV2({
7393
- model,
7394
- value: queryText,
7395
- maxRetries,
7396
- // Type assertion needed: providerOptions type is a union, but embedV2 expects specific version
7397
- ...providerOptions && { providerOptions }
7398
- });
7399
- } else {
7400
- embeddingResult = await vector.embedV1({
7401
- value: queryText,
7402
- model,
7403
- maxRetries
7404
- });
7444
+ try {
7445
+ if (model.specificationVersion === "v3") {
7446
+ embeddingResult = await vector.embedV3({
7447
+ model,
7448
+ value: queryText,
7449
+ maxRetries,
7450
+ // Type assertion needed: providerOptions type is a union, but embedV3 expects specific version
7451
+ ...providerOptions && {
7452
+ providerOptions
7453
+ }
7454
+ });
7455
+ } else if (model.specificationVersion === "v2") {
7456
+ embeddingResult = await vector.embedV2({
7457
+ model,
7458
+ value: queryText,
7459
+ maxRetries,
7460
+ // Type assertion needed: providerOptions type is a union, but embedV2 expects specific version
7461
+ ...providerOptions && {
7462
+ providerOptions
7463
+ }
7464
+ });
7465
+ } else {
7466
+ embeddingResult = await vector.embedV1({
7467
+ value: queryText,
7468
+ model,
7469
+ maxRetries
7470
+ });
7471
+ }
7472
+ } catch (err) {
7473
+ embedSpan?.error({ error: err, endSpan: true });
7474
+ throw err;
7405
7475
  }
7406
7476
  const embedding = embeddingResult.embedding;
7477
+ const embedUsage = embeddingResult?.usage;
7478
+ embedSpan?.end({
7479
+ attributes: {
7480
+ dimensions: embedding?.length,
7481
+ ...embedUsage && {
7482
+ usage: {
7483
+ inputTokens: embedUsage.tokens ?? embedUsage.promptTokens ?? embedUsage.inputTokens
7484
+ }
7485
+ }
7486
+ },
7487
+ output: { dimensions: embedding?.length }
7488
+ });
7407
7489
  const queryParams = {
7408
7490
  indexName,
7409
7491
  queryVector: embedding,
@@ -7411,7 +7493,29 @@ var vectorQuerySearch = async ({
7411
7493
  filter: queryFilter,
7412
7494
  includeVector: includeVectors
7413
7495
  };
7414
- const results = await vectorStore.query({ ...queryParams, ...databaseSpecificParams(databaseConfig) });
7496
+ const querySpan = parentSpan?.createChildSpan({
7497
+ type: observability.SpanType.RAG_VECTOR_OPERATION,
7498
+ name: `rag vector: query`,
7499
+ // Pass filter as-is; the observability layer's deepClean handles
7500
+ // size limits and sanitization centrally.
7501
+ input: { topK, filter: queryFilter },
7502
+ attributes: {
7503
+ operation: "query",
7504
+ indexName,
7505
+ topK,
7506
+ dimensions: embedding?.length
7507
+ }
7508
+ });
7509
+ let results;
7510
+ try {
7511
+ results = await vectorStore.query({ ...queryParams, ...databaseSpecificParams(databaseConfig) });
7512
+ } catch (err) {
7513
+ querySpan?.error({ error: err, endSpan: true });
7514
+ throw err;
7515
+ }
7516
+ querySpan?.end({
7517
+ output: { returned: results?.length ?? 0 }
7518
+ });
7415
7519
  return { results, queryEmbedding: embedding };
7416
7520
  };
7417
7521
  var databaseSpecificParams = (databaseConfig) => {
@@ -7675,7 +7779,9 @@ var createGraphRAGTool = (options) => {
7675
7779
  outputSchema,
7676
7780
  description: toolDescription,
7677
7781
  execute: async (inputData, context) => {
7678
- const { requestContext, mastra } = context || {};
7782
+ const { requestContext, mastra, tracingContext } = context || {};
7783
+ const observabilityContext = observability.createObservabilityContext(tracingContext);
7784
+ const parentSpan = observabilityContext.tracingContext?.currentSpan;
7679
7785
  const indexName = requestContext?.get("indexName") ?? options.indexName;
7680
7786
  const vectorStoreName = "vectorStore" in options ? storeName : requestContext?.get("vectorStoreName") ?? storeName;
7681
7787
  if (!indexName) throw new Error(`indexName is required, got: ${indexName}`);
@@ -7713,7 +7819,8 @@ var createGraphRAGTool = (options) => {
7713
7819
  queryFilter: Object.keys(queryFilter || {}).length > 0 ? queryFilter : void 0,
7714
7820
  topK: topKValue,
7715
7821
  includeVectors: true,
7716
- providerOptions
7822
+ providerOptions,
7823
+ observabilityContext
7717
7824
  });
7718
7825
  if (logger) {
7719
7826
  logger.debug("vectorQuerySearch returned results", { count: results.length });
@@ -7729,17 +7836,50 @@ var createGraphRAGTool = (options) => {
7729
7836
  if (logger) {
7730
7837
  logger.debug("Initializing graph", { chunkCount: chunks.length, embeddingCount: embeddings.length });
7731
7838
  }
7732
- graphRag.createGraph(chunks, embeddings);
7839
+ const buildSpan = parentSpan?.createChildSpan({
7840
+ type: observability.SpanType.GRAPH_ACTION,
7841
+ name: "graph build",
7842
+ input: { nodeCount: chunks.length },
7843
+ attributes: {
7844
+ action: "build",
7845
+ nodeCount: chunks.length,
7846
+ threshold: graphOptions.threshold
7847
+ }
7848
+ });
7849
+ try {
7850
+ graphRag.createGraph(chunks, embeddings);
7851
+ } catch (err) {
7852
+ buildSpan?.error({ error: err, endSpan: true });
7853
+ throw err;
7854
+ }
7855
+ buildSpan?.end();
7733
7856
  isInitialized = true;
7734
7857
  } else if (logger) {
7735
7858
  logger.debug("Graph already initialized, skipping graph construction");
7736
7859
  }
7737
- const rerankedResults = graphRag.query({
7738
- query: queryEmbedding,
7739
- topK: topKValue,
7740
- randomWalkSteps,
7741
- restartProb
7860
+ const traverseSpan = parentSpan?.createChildSpan({
7861
+ type: observability.SpanType.GRAPH_ACTION,
7862
+ name: "graph traverse",
7863
+ input: { topK: topKValue, randomWalkSteps, restartProb },
7864
+ attributes: {
7865
+ action: "traverse",
7866
+ startNodes: 1,
7867
+ maxDepth: randomWalkSteps
7868
+ }
7742
7869
  });
7870
+ let rerankedResults;
7871
+ try {
7872
+ rerankedResults = graphRag.query({
7873
+ query: queryEmbedding,
7874
+ topK: topKValue,
7875
+ randomWalkSteps,
7876
+ restartProb
7877
+ });
7878
+ } catch (err) {
7879
+ traverseSpan?.error({ error: err, endSpan: true });
7880
+ throw err;
7881
+ }
7882
+ traverseSpan?.end({ output: { returned: rerankedResults.length } });
7743
7883
  if (logger) {
7744
7884
  logger.debug("GraphRAG query returned results", { count: rerankedResults.length });
7745
7885
  }
@@ -7778,7 +7918,8 @@ var createVectorQueryTool = (options) => {
7778
7918
  inputSchema,
7779
7919
  outputSchema,
7780
7920
  execute: async (inputData, context) => {
7781
- const { requestContext, mastra } = context || {};
7921
+ const { requestContext, mastra, tracingContext } = context || {};
7922
+ const observabilityContext = observability.createObservabilityContext(tracingContext);
7782
7923
  const indexName = requestContext?.get("indexName") ?? options.indexName;
7783
7924
  const vectorStoreName = "vectorStore" in options ? storeName : requestContext?.get("vectorStoreName") ?? storeName;
7784
7925
  const includeVectors = requestContext?.get("includeVectors") ?? options.includeVectors ?? false;
@@ -7819,7 +7960,8 @@ var createVectorQueryTool = (options) => {
7819
7960
  topK: topKValue,
7820
7961
  includeVectors,
7821
7962
  databaseConfig,
7822
- providerOptions
7963
+ providerOptions,
7964
+ observabilityContext
7823
7965
  });
7824
7966
  if (logger) {
7825
7967
  logger.debug("vectorQuerySearch returned results", { count: results.length });
@@ -7836,13 +7978,15 @@ var createVectorQueryTool = (options) => {
7836
7978
  scorer: reranker.model,
7837
7979
  options: {
7838
7980
  ...reranker.options,
7839
- topK: reranker.options?.topK || topKValue
7981
+ topK: reranker.options?.topK || topKValue,
7982
+ observabilityContext
7840
7983
  }
7841
7984
  });
7842
7985
  } else {
7843
7986
  rerankedResults = await rerank(results, queryText, reranker.model, {
7844
7987
  ...reranker.options,
7845
- topK: reranker.options?.topK || topKValue
7988
+ topK: reranker.options?.topK || topKValue,
7989
+ observabilityContext
7846
7990
  });
7847
7991
  }
7848
7992
  if (logger) {