Repository navigation
fix(agentchat): make TokenUsageTermination component config loadable when a token limit is unset - #8426
Open
DZH166 wants to merge 1 commit into
Open
fix(agentchat): make TokenUsageTermination component config loadable when a token limit is unset#8426DZH166 wants to merge 1 commit into
DZH166 wants to merge 1 commit into
Conversation
…when a token limit is unset
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why are these changes needed?
TokenUsageTerminationcannot survive a declarativedump_component()/load_component()roundtrip unless all three token limits are set. For example:Root cause:
dump_component()serializes the config withmodel_dump(exclude_none=True), so any limit left asNoneis omitted from the dumped dictionary — butTokenUsageTerminationConfigdeclaredmax_total_token,max_prompt_token, andmax_completion_tokenas required fields (no default). Since the constructor allows omitting any subset of the limits (only "at least one" is required), the common single-limit usage produces a component config that can never be loaded back. Nested insideOr/Andtermination conditions it fails the same way, so saved teams usingTokenUsageTerminationwith a partial limit are not restorable.Fix: give the three config fields
= Nonedefaults, matching the constructor signature (max_total_token: int | None = None, etc.). Omitted fields now validate back toNone, and the constructor's "at least one limit required" check continues to reject genuinely invalid (all-None) configs. Previously dumped configs that omitted unset limits become loadable again.Related issue number
Self-found bug (no existing issue); repro and root-cause analysis included above.
Checks
test_token_usage_termination_component_roundtripinpython/packages/autogen-agentchat/tests/test_termination_condition.py; red before the fix (ValidationError), green after (14/14 in the file).ruff check/ruff format --checkclean on both changed files;git diff --checkclean;tests/test_declarative_components.pystill passes.Validation
python -m pytest tests/test_termination_condition.py→1 failed, 13 passed(the new roundtrip test fails withpydantic ValidationError: Field requiredfromComponentLoader.load_component).14 passed.{max_total_token, max_prompt_token, max_completion_token}now roundtrip (pre-fix: 7 of 8 combos failed, including nestedOrTerminationConditionconfigs).AI assistance
This contribution was prepared with AI assistance (code localization, patch drafting, and test authoring), reviewed and validated by the submitter. The bug was identified and the fix verified locally against the repository's own test suite.