You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Proposal: OpenTelemetry spans for RAG retrieval stages and fallbacks
#4331
When a RAG response is slow or uses fewer sources than expected, the current trace does not show whether the time went to embedding, a vector store, or the optional reranker. It also does not show when one knowledge base was skipped or when reranking fell back to vector order. The existing tracing middleware traces agent replies, model calls, and tool execution. Static RAG and the agentic search_knowledge tool both call _search_across, which calls KnowledgeBase.search for embedding and vector search, and invokes the reranker directly.
A small scenario to illustrate the gap: use two fake knowledge bases, let one return hits and the other raise, then have the fake reranker raise. The request still gets usable results in vector order. The trace should show which knowledge base failed and why the result used the fallback, without needing to correlate separate warning logs.
Proposed first PR
Reuse the existing TracingMiddleware opt-in and OTel provider. Add one retrieval span under the active reply span (static mode) or tool span (agentic mode), one child per knowledge-base search, and an optional rerank child. Under each built-in knowledge-base span, distinguish the embedding call from the vector-search phase. Keep the concurrent vector queries in one phase span per knowledge base to bound span volume. Only create a stage span when that stage actually runs. Preserve parentage across async tasks; keep spans scoped to the awaited work.
Use the OTel GenAI retrieval convention where applicable. Use span duration and record only operational metadata such as knowledge-base position within the search, query/candidate/hit counts, requested top_k, outcome, and error type. The new spans would omit query text, prompts, chunks, vectors, storage identifiers, and raw exception messages or stack traces.
Existing behavior to preserve
One knowledge base fails, another has hits: mark the failed child as an error and the retrieval as partial. Merge and return the surviving hits as today.
A knowledge base fails and no hits survive: mark retrieval as an error. Preserve the current behavior: static mode continues without a hint, and the agentic tool returns an error. A successful search with zero hits remains a normal empty result.
Reranking fails: mark the rerank child as an error, mark the fallback on retrieval, and return the existing vector order truncated to top_k. Cancellation still propagates and is distinguishable from a handled fallback.
Regression plan
Use fake stores, embeddings and a fake reranker with the existing InMemorySpanExporter setup. Assert the span hierarchy in both modes, including sibling spans for concurrent knowledge-base searches. Assert identical results and ordering with and without tracing. Cover a partial failure, failure with no surviving hits, a normal empty result, rerank failure, and cancellation. Check that skipped stages do not emit spans, started spans close during cleanup, and sentinel query text, chunk text, and exception messages never appear on the new spans. No external services or model keys are needed.
This is about spans within retrieval. #1835 is about enabling and exporting tracing at the app level, and #4291 proposes a different ranking strategy. This would leave tracing setup and ranking behavior unchanged.
Would this scope fit AgentScope's tracing design? I’d be happy to implement it once maintainers confirm the direction.
Codex assisted with the source review and drafting.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
What is missing today?
When a RAG response is slow or uses fewer sources than expected, the current trace does not show whether the time went to embedding, a vector store, or the optional reranker. It also does not show when one knowledge base was skipped or when reranking fell back to vector order. The existing tracing middleware traces agent replies, model calls, and tool execution. Static RAG and the agentic
search_knowledgetool both call_search_across, which callsKnowledgeBase.searchfor embedding and vector search, and invokes the reranker directly.A small scenario to illustrate the gap: use two fake knowledge bases, let one return hits and the other raise, then have the fake reranker raise. The request still gets usable results in vector order. The trace should show which knowledge base failed and why the result used the fallback, without needing to correlate separate warning logs.
Proposed first PR
Reuse the existing
TracingMiddlewareopt-in and OTel provider. Add one retrieval span under the active reply span (static mode) or tool span (agentic mode), one child per knowledge-base search, and an optional rerank child. Under each built-in knowledge-base span, distinguish the embedding call from the vector-search phase. Keep the concurrent vector queries in one phase span per knowledge base to bound span volume. Only create a stage span when that stage actually runs. Preserve parentage across async tasks; keep spans scoped to the awaited work.Use the OTel GenAI retrieval convention where applicable. Use span duration and record only operational metadata such as knowledge-base position within the search, query/candidate/hit counts, requested
top_k, outcome, and error type. The new spans would omit query text, prompts, chunks, vectors, storage identifiers, and raw exception messages or stack traces.Existing behavior to preserve
top_k. Cancellation still propagates and is distinguishable from a handled fallback.Regression plan
Use fake stores, embeddings and a fake reranker with the existing
InMemorySpanExportersetup. Assert the span hierarchy in both modes, including sibling spans for concurrent knowledge-base searches. Assert identical results and ordering with and without tracing. Cover a partial failure, failure with no surviving hits, a normal empty result, rerank failure, and cancellation. Check that skipped stages do not emit spans, started spans close during cleanup, and sentinel query text, chunk text, and exception messages never appear on the new spans. No external services or model keys are needed.This is about spans within retrieval. #1835 is about enabling and exporting tracing at the app level, and #4291 proposes a different ranking strategy. This would leave tracing setup and ranking behavior unchanged.
Would this scope fit AgentScope's tracing design? I’d be happy to implement it once maintainers confirm the direction.
Codex assisted with the source review and drafting.
All reactions