Add PerModelCapability for model-specific capability routing#4969
Add PerModelCapability for model-specific capability routing#4969
Conversation
A new generic capability that delegates to different implementations based on the model. Accepts a routes dict mapping model class or system string to capability instance, with configurable fallback behavior (error, ignore, or custom capability). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Remove PerModelCapability (routed.py, tests, docs) from this branch. It will be merged independently via #4969, keeping this PR focused on compaction support only. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
| }, | ||
| fallback='ignore', # silently skip unsupported models | ||
| ) | ||
| agent = Agent('openai:gpt-4o', capabilities=[cap]) |
There was a problem hiding this comment.
🔴 Doc example uses outdated model gpt-4o instead of frontier model gpt-5.2
Line 776 uses 'openai:gpt-4o' while every other example in the same file (lines 78, 99, 176, 206, 259, 302, 342, 376, 460, 566, 741, 833, 889) uses 'openai:gpt-5.2'. This violates the mandatory rule in agent_docs/index.md (rule:-2): "Use latest frontier models (e.g. openai:gpt-5.2, anthropic:claude-opus-4-6) in docs/examples — Outdated model references make our product look unmaintained and reduce user trust."
| agent = Agent('openai:gpt-4o', capabilities=[cap]) | |
| agent = Agent('openai:gpt-5.2', capabilities=[cap]) |
Was this helpful? React with 👍 or 👎 to provide feedback.
|
|
||
| The `fallback` parameter controls what happens when no route matches: | ||
|
|
||
| - `None` (default) — raise a `UserError` |
There was a problem hiding this comment.
🔴 UserError not linked with MkDocs reference syntax in docs prose
Line 783 mentions `UserError` as plain backtick text, but per docs/.cursor/rules.mdc ("Always reference the python code in the docs e.g. ModelSettings should link to [ModelSettings][pydantic_ai.settings.ModelSettings]") and docs/AGENTS.md rule:66, all API elements must use reference-style links. The same file already links UserError correctly at docs/capabilities.md:144 as [`UserError`][pydantic_ai.exceptions.UserError].
| - `None` (default) — raise a `UserError` | |
| - `None` (default) — raise a [`UserError`][pydantic_ai.exceptions.UserError] |
Was this helpful? React with 👍 or 👎 to provide feedback.
Docs Preview
|
| routes={ | ||
| 'openai': my_openai_capability, | ||
| 'anthropic': my_anthropic_capability, | ||
| }, |
There was a problem hiding this comment.
I think this can be part of existing Capability primitive:
class Capability():
for: str | None = None# returns capability that only works with Anthropic
CodeExecutionCapability().for("anthropic")
# OR pass a function that decides whether to use that capability or not
# useful for feature flags / dynamic capability injection
def feature_flag(ctx: RunContext):
return is_feature_enabled("code-execution")
CodeExecutionCapability().for(feature_flag)What do you think?
Summary
PerModelCapability, a generic capability that routes to different implementations based on the modelroutesdict mapping model class or system string to capability instancefallback:None(error, default),'ignore'(no-op), or a capability instanceWrapperModel, rejectsFallbackModelfor_runon matched/fallback capabilitiesOpenAICompactionandAnthropicCompactioncapabilities #4943 (compaction support) for independent reviewTest plan
🤖 Generated with Claude Code