Checked other resources
Package (Required)
Feature Description
ModelRequest contains a runtime: Runtime[Context] parameter, but ModelRequest itself is not generic over the Context type. This forces developers to cast request.runtime.context to their specific context type when implementing middleware, breaking type safety.
The same issue applies to ModelResponse and the AgentMiddleware base class - they should all be generic over Context to provide proper type checking throughout the middleware chain.
Use Case
When implementing custom middleware with typed contexts, developers are forced to cast:
@dataclass
class MyContext:
user_id: str
session_data: dict
class MyMiddleware(AgentMiddleware):
async def awrap_model_call(
self,
request: ModelRequest, # Not generic over Context
handler: Callable[[ModelRequest], Awaitable[ModelResponse]],
) -> ModelCallResult:
# Have to cast - no type safety!
context = cast(MyContext, request.runtime.context)
user_id = context.user_id # IDE can't autocomplete, type checker can't validate
response = await handler(request)
return response
This breaks type safety because:
- IDE autocomplete doesn't work for
context attributes
- Type checkers (mypy, pyright) can't validate context usage
- Refactoring context types is error-prone
- Runtime errors possible if cast is incorrect
Proposed Solution
No response
Alternatives Considered
No response
Additional Context
No response
Checked other resources
Package (Required)
Feature Description
ModelRequestcontains aruntime: Runtime[Context]parameter, butModelRequestitself is not generic over theContexttype. This forces developers to castrequest.runtime.contextto their specific context type when implementing middleware, breaking type safety.The same issue applies to
ModelResponseand theAgentMiddlewarebase class - they should all be generic overContextto provide proper type checking throughout the middleware chain.Use Case
When implementing custom middleware with typed contexts, developers are forced to cast:
This breaks type safety because:
contextattributesProposed Solution
No response
Alternatives Considered
No response
Additional Context
No response