Skip to content

feat: add internal tracing to components that use chat generators - #12075

Merged
sjrl merged 7 commits into
mainfrom
add-usage-tracking-internal-chat-gen
Jul 21, 2026
Merged

feat: add internal tracing to components that use chat generators#12075
sjrl merged 7 commits into
mainfrom
add-usage-tracking-internal-chat-gen

Conversation

@sjrl

@sjrl sjrl commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

LLMEvaluator, LLMRanker, QueryExpander, LLMMetadataExtractor, LLMDocumentContentExtractor and LLMMessagesRouter now wrap their internal ChatGenerator calls in a haystack.chat_generator.run tracing span. These components do not return ChatMessage objects, so the LLM token usage carried in reply.meta["usage"] was previously lost to tracers. The new span exposes the generator's replies via the haystack.component.output tag, so token usage is now visible in traces (requires content tracing to be enabled). When the generator runs across threads, the span is nested under the component's span.

How did you test it?

New tests

Notes for the reviewer

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

@vercel

vercel Bot commented Jul 20, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
haystack-docs Ready Ready Preview, Comment Jul 21, 2026 9:01am

Request Review

@github-actions github-actions Bot added topic:tests type:documentation Improvements on the docs labels Jul 20, 2026
@github-actions

github-actions Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/evaluators
  llm_evaluator.py 241
  haystack/components/extractors
  llm_metadata_extractor.py 325-327, 333, 347-349
  haystack/components/extractors/image
  llm_document_content_extractor.py
  haystack/components/generators
  utils.py
  haystack/components/query
  query_expander.py 289
  haystack/components/rankers
  llm_ranker.py
  haystack/components/routers
  llm_messages_router.py 192
  haystack/core/pipeline
  pipeline.py
Project Total  

This report was generated by python-coverage-comment-action

Comment on lines +385 to +389
# Capture the current span here so worker threads nest their generator spans under the component span.
parent_span = tracing.tracer.current_span()

with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
results = executor.map(self._run_on_thread, image_contents)
results = executor.map(partial(self._run_on_thread, parent_span=parent_span), image_contents)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note we only directly pass down a parent_span in the cases where the chat generator is being run in a separate thread or in an async context.

@sjrl
sjrl marked this pull request as ready for review July 21, 2026 07:06
@sjrl
sjrl requested a review from a team as a code owner July 21, 2026 07:06
@sjrl
sjrl requested review from davidsbatista and removed request for a team July 21, 2026 07:06
Comment thread haystack/components/rankers/llm_ranker.py Outdated

@davidsbatista davidsbatista left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small suggestion. Looks good 👍🏽

sjrl added 2 commits July 21, 2026 10:58
…pset-ai/haystack into add-usage-tracking-internal-chat-gen
@sjrl
sjrl enabled auto-merge (squash) July 21, 2026 08:59
@sjrl
sjrl merged commit 13cab92 into main Jul 21, 2026
24 checks passed
@sjrl
sjrl deleted the add-usage-tracking-internal-chat-gen branch July 21, 2026 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants