Skip to content

feat(agents): add ratio history trimmer + history_processors wiring#81

Open
sanzog03 wants to merge 1 commit into
developfrom
feature/ratio_trimmer_capability
Open

feat(agents): add ratio history trimmer + history_processors wiring#81
sanzog03 wants to merge 1 commit into
developfrom
feature/ratio_trimmer_capability

Conversation

@sanzog03
Copy link
Copy Markdown
Collaborator

Summary

Adds history_processors=[...] on super().init() alongside the existing capabilities=[...] spread, backed by a new
_build_history_processors_from_scalars() builder and a make_ratio_trimmer factory.

What it does

  • PydanticAIBaseAgent now respects config.enable_trimming + config.trim_ratio — set them, and the agent automatically installs a ratio-based history trimmer that drops the oldest fraction
    of messages on every model request while preserving the first message (usually the system prompt).
  • Consumers can also supply their own history processors directly via config.history_processors (new pass-through list, same shape as config.capabilities).
  • Trimming stays opt-in by default. PydanticAIBaseAgentConfig overrides BaseAgentConfig.enable_trimming to False — the naive ratio trimmer can strand ToolReturnParts paired with earlier
    ToolCallParts, which OpenAI's API rejects. Safe opt-in once the caller knows their history shape is OK.
  • Subclass-friendly: _build_history_processors_from_scalars follows the same override + super() pattern as _build_capabilities_from_scalars.

Files Changed:

  • akd_ext/agents/_base/pydantic_ai/_capabilities.py — make_ratio_trimmer factory + module docstring noting the mode of future additions.
  • akd_ext/agents/_base/pydantic_ai/_base.py — imports make_ratio_trimmer; adds history_processors: list[Any] and enable_trimming: bool = False override on PydanticAIBaseAgentConfig; adds
    the history_processors=[...] kwarg in super().init(); adds _build_history_processors_from_scalars() next to _build_capabilities_from_scalars.
  • tests/agents/test_base_pydantic.py — adds seven tests under a new "make_ratio_trimmer" section.

Testing

Hermetic test suite (no network, no model keys):

uv run pytest tests/agents/test_base_pydantic.py -n=3

@sanzog03 sanzog03 requested a review from NISH1001 April 23, 2026 19:39
Base automatically changed from feature/pydantic_ai_base_agent to develop April 27, 2026 21:05
@NISH1001
Copy link
Copy Markdown
Collaborator

I was thinking of leaning towards:
https://github.com/vstorm-co/summarization-pydantic-ai

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants