`pydantic-ai-middleware` was a library providing a simple middleware layer for Pydantic-AI agents, allowing for `before_run`/`after_run` hooks and other lifecycle management without imposing strict guardrails. The last version of this package was `0.2.4`. As of version `0.3.0`, the package was renamed to `pydantic-ai-shields`, and its core middleware functionality was integrated directly into `pydantic-ai` v1.71+.
pip install pydantic-ai-middleware==0.2.4Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to wrap a `pydantic-ai` agent with custom middleware using `MiddlewareAgent` and `AgentMiddleware` from `pydantic-ai-middleware` v0.2.4. It defines a simple middleware to log before and after agent execution.
For new projects, use `pydantic-ai-shields` for guardrails or `pydantic-ai` v1.71+ for native agent middleware. For existing projects, downgrade to `pydantic-ai-middleware<0.3.0` or migrate to the new `pydantic-ai` native middleware.
Ensure `pydantic-ai-slim` is installed instead of `pydantic-ai`. Imports for `pydantic_ai.*` remain compatible.
Upgrade `pydantic-ai-middleware` to version `0.2.4` or higher. If unable to upgrade, you may need to downgrade `pydantic-ai-slim` to a version that predates the `description` abstract property.
Upgrade `pydantic-ai-middleware` to version `0.2.3` or higher to ensure all middleware hooks are correctly triggered during streaming operations.