Skip to content

feat: add output token limit utils for chat generators - #12305

Closed
sjrl wants to merge 4 commits into
mainfrom
feat/output-token-limit-utils
Closed

feat: add output token limit utils for chat generators#12305
sjrl wants to merge 4 commits into
mainfrom
feat/output-token-limit-utils

Conversation

@sjrl

@sjrl sjrl commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

Add util method for resolving how to pass down a max token limit from the Summarization Compactor down to the Chat Generator. We need to know the token limit at the Compactor level so we can properly account for how large the expected summary is when computing how much of the previous chat history should be summarized.

The method I chose here is to create a util method that allows us to pass down a max token limit through the generation_kwargs runtime params of the ChatGenerator.

However, I am wondering if a better solution would be to directly expose this as a standardized param name on the ChatGenerators run method instead of needing this util method.

How did you test it?

Added new tests.

Notes for the reviewer

I made this a standalone PR so its easier to discuss whether we like this solution and to reduce the PR size of the eventual summarization compactor.

I'd appreciate your thoughts on whether it makes sense to open issues to update our ChatGenerators to use a standardized param name like max_output_tokens instead of needing to maintain/update this dict.

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.

@sjrl sjrl self-assigned this Aug 11, 2026
@vercel

vercel Bot commented Aug 11, 2026

Copy link
Copy Markdown

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
haystack-docs Ignored Ignored Preview Aug 12, 2026 7:24am

Request Review

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

github-actions Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/generators/chat
  utils.py
Project Total  

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

@sjrl sjrl added the ignore-for-release-notes PRs with this flag won't be included in the release notes. label Aug 11, 2026
@sjrl
sjrl marked this pull request as ready for review August 12, 2026 07:28
@sjrl
sjrl requested a review from a team as a code owner August 12, 2026 07:28
@sjrl
sjrl requested review from julian-risch and removed request for a team August 12, 2026 07:28
@anakin87

Copy link
Copy Markdown
Member

The current idea assumes that Haystack knows how each provider names this parameter.

A potential solution I'd prefer: provide each ChatGenerator with a public field like output_token_limit_key that can be queried: MyChatGenerator.output_token_limit_key.

Another idea, as you also suggest, is to add this param to the run method: more explicit but we would then need to handle conflicts between this param and generation_kwargs; it seems to me that this would require more extensive changes.

What do you think?

@sjrl

sjrl commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

The current idea assumes that Haystack knows how each provider names this parameter.

A potential solution I'd prefer: provide each ChatGenerator with a public field like output_token_limit_key that can be queried: MyChatGenerator.output_token_limit_key.

I think this could work. However, I would want to set this up in such a way that it can be easily extended later to other keys. If this is set up well we could use this for passing down other common generation kwargs in a standardized way like temperature, reasoning level, etc.

Another idea, as you also suggest, is to add this param to the run method: more explicit but we would then need to handle conflicts between this param and generation_kwargs; it seems to me that this would require more extensive changes.

Yeah I agree this would cause a conflict and also I'm not sure if I want to greatly expand the run method signature (thinking of what I mention above about adding more keys in the future).

What do you think?

I agree that I would much prefer having this info stored on the specific implementation itself. Do you think we could take your first idea and make it more extensible?

@anakin87

Copy link
Copy Markdown
Member

I agree that I would much prefer having this info stored on the specific implementation itself. Do you think we could take your first idea and make it more extensible?

I think yes. We could potentially store a dictionary in the ChatGenerator with the mapping: Haystack concept -> Provider-specific key. (Just an idea, haven't tried)

If we have doubts about the implementation and we decide to do this, I'd just make the field(s) private for the moment.

@sjrl

sjrl commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

I agree that I would much prefer having this info stored on the specific implementation itself. Do you think we could take your first idea and make it more extensible?

I think yes. We could potentially store a dictionary in the ChatGenerator with the mapping: Haystack concept -> Provider-specific key. (Just an idea, haven't tried)

If we have doubts about the implementation and we decide to do this, I'd just make the field(s) private for the moment.

Okay sounds good, I'll take a look into this!

@sjrl

sjrl commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

Moving back to draft while looking into a new solution

@sjrl

sjrl commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

Closing as superseded by #12328

@sjrl sjrl closed this Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ignore-for-release-notes PRs with this flag won't be included in the release notes. topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants