{"id":10087,"library":"pydantic-ai-middleware","title":"pydantic-ai-middleware (Abandoned: Renamed to pydantic-ai-shields)","description":"`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+.","status":"abandoned","version":"0.2.4","language":"en","source_language":"en","source_url":"https://github.com/vstorm-co/pydantic-ai-middleware","tags":["pydantic-ai","middleware","agent","hooks","abandoned","renamed"],"install":[{"cmd":"pip install pydantic-ai-middleware==0.2.4","lang":"bash","label":"Install last version of pydantic-ai-middleware"}],"dependencies":[{"reason":"Core dependency for Pydantic-AI agent functionalities. Replaced `pydantic-ai` in v0.2.2.","package":"pydantic-ai-slim","optional":false},{"reason":"Required for `CostTrackingMiddleware` to calculate USD costs.","package":"genai-prices","optional":true}],"imports":[{"note":"This class was removed from `pydantic-ai-middleware` v0.3.0+ (now `pydantic-ai-shields`) and native support was added to `pydantic-ai` v1.71+","wrong":"from pydantic_ai_shields.agent import MiddlewareAgent","symbol":"MiddlewareAgent","correct":"from pydantic_ai_middleware.agent import MiddlewareAgent"},{"note":"This base class was removed from `pydantic-ai-middleware` v0.3.0+ (now `pydantic-ai-shields`) and native support was added to `pydantic-ai` v1.71+","wrong":"from pydantic_ai_shields.middleware import AgentMiddleware","symbol":"AgentMiddleware","correct":"from pydantic_ai_middleware.middleware import AgentMiddleware"},{"note":"This specific middleware was moved to `pydantic-ai-shields` as of `pydantic-ai-middleware` v0.3.0","wrong":"from pydantic_ai_shields.middleware import CostTrackingMiddleware","symbol":"CostTrackingMiddleware","correct":"from pydantic_ai_middleware.middleware import CostTrackingMiddleware"},{"note":"This class was moved to `pydantic-ai-shields` as of `pydantic-ai-middleware` v0.3.0","wrong":"from pydantic_ai_shields.chain import MiddlewareChain","symbol":"MiddlewareChain","correct":"from pydantic_ai_middleware.chain import MiddlewareChain"}],"quickstart":{"code":"import os\nfrom pydantic_ai import PydanticAI\nfrom pydantic_ai.agents import Agent\nfrom pydantic_ai_middleware.agent import MiddlewareAgent\nfrom pydantic_ai_middleware.middleware import AgentMiddleware\nimport asyncio\n\nclass MyCustomMiddleware(AgentMiddleware):\n    async def before_run(self, agent: Agent, input_data: str, **kwargs):\n        print(f\"Middleware: Before run with input: {input_data}\")\n        return input_data\n\n    async def after_run(self, agent: Agent, output_data: str, **kwargs):\n        print(f\"Middleware: After run with output: {output_data}\")\n        return output_data\n\nclass MyAgent(Agent):\n    def run(self, topic: str) -> str:\n        \"\"\"Generate a short story about the given topic.\"\"\"\n        return f\"A story about {topic}...\"\n\nai = PydanticAI(\n    api_key=os.environ.get(\"OPENAI_API_KEY\", \"sk-DUMMY\"), # Use a dummy key if not set\n    model=\"gpt-3.5-turbo\",\n)\nmy_agent = MyAgent(ai=ai)\n\nmiddleware_agent = MiddlewareAgent(\n    wrapped_agent=my_agent,\n    middleware=[MyCustomMiddleware()]\n)\n\nasync def main():\n    print(\"\\n--- Running Middleware Agent ---\")\n    result = await middleware_agent.run(\"a brave knight\")\n    print(f\"Agent final result: {result}\")\n    print(\"--------------------------------\")\n\nif __name__ == \"__main__\":\n    asyncio.run(main())\n","lang":"python","description":"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."},"warnings":[{"fix":"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.","message":"The `pydantic-ai-middleware` package has been renamed to `pydantic-ai-shields` as of version `0.3.0`. The core middleware functionality (`MiddlewareAgent`, `AgentMiddleware`) has been removed from this package and integrated directly into `pydantic-ai` v1.71+.","severity":"breaking","affected_versions":">=0.3.0"},{"fix":"Ensure `pydantic-ai-slim` is installed instead of `pydantic-ai`. Imports for `pydantic_ai.*` remain compatible.","message":"The `pydantic-ai` dependency was replaced with `pydantic-ai-slim` in version `0.2.2` to reduce dependency bloat. While `pydantic-ai` still works with older versions of `pydantic-ai-middleware`, using `pydantic-ai-slim` is recommended.","severity":"deprecated","affected_versions":"0.2.2+"},{"fix":"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.","message":"Instances of `MiddlewareAgent` will fail to initialize with `AttributeError: Can't instantiate abstract class MiddlewareAgent with abstract method description` when used with `pydantic-ai` versions that introduced `description` as an abstract property on `AbstractAgent` (e.g., `pydantic-ai-slim` >= 1.73.0).","severity":"gotcha","affected_versions":"0.2.0 - 0.2.3"},{"fix":"Upgrade `pydantic-ai-middleware` to version `0.2.3` or higher to ensure all middleware hooks are correctly triggered during streaming operations.","message":"When using `MiddlewareAgent.iter()` or `run_stream()`, `after_run` and `on_error` lifecycle hooks were not invoked in `AgentMiddleware`, making post-processing or error handling impossible for streaming operations.","severity":"gotcha","affected_versions":"0.2.0 - 0.2.2"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Install `pydantic-ai-middleware==0.2.4` for the old functionality, or migrate to `pydantic-ai-shields` (for guardrails) or `pydantic-ai` v1.71+ (for native agent middleware).","cause":"Attempting to import from `pydantic-ai-middleware` after version `0.3.0`, which was renamed to `pydantic-ai-shields` and had its core middleware removed.","error":"ModuleNotFoundError: No module named 'pydantic_ai_middleware.agent'"},{"fix":"Upgrade `pydantic-ai-middleware` to `0.2.4`. If this is not possible, downgrade `pydantic-ai-slim` to a version where `description` is not an abstract method on `AbstractAgent`.","cause":"Using `MiddlewareAgent` from `pydantic-ai-middleware` versions `0.2.0-0.2.3` with a `pydantic-ai-slim` version (e.g., >=1.73.0) that made `description` an abstract property on `AbstractAgent`.","error":"AttributeError: Can't instantiate abstract class MiddlewareAgent with abstract method description"},{"fix":"Upgrade `pydantic-ai-middleware` to version `0.2.3` or higher.","cause":"Prior to version `0.2.3`, `AgentMiddleware`'s `after_run` and `on_error` hooks were not correctly invoked when using `MiddlewareAgent.iter()` or `run_stream()`.","error":"TypeError: 'NoneType' object is not callable (or similar error during streaming/iteration)"}]}