Repository navigation
Conversation
_toolset.py imported pydantic_ai.mcp at module level for RestateMCPToolset, so importing the extension failed unless pydantic-ai-slim[mcp] was installed, even for agents with no MCP toolsets. Move RestateMCPToolset into its own module, which RestateAgent already imports lazily inside its ImportError guard. Fixes restatedev#221
|
All contributors have signed the CLA ✍️ ✅ |
Author
|
I have read the CLA Document and I hereby sign the CLA |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #221.
_toolset.pyimportedpydantic_ai.mcpat module level forRestateMCPToolset, andpydantic_ai.mcpraisesImportErrorwhen the MCP client packages aren't installed. Becauserestate/ext/pydantic/__init__.pyimports_toolset, the whole extension failed to import unlesspydantic-ai-slim[mcp]was installed, even for agents that only use function tools.RestateAgentalready importsRestateMCPToolsetlazily inside atry/except ImportError, but the guard never got a chance to work because_toolsetwas imported unconditionally a few lines above it. So I movedRestateMCPToolsetand its two serde wrappers into a new_mcp_toolset.py, and pointed the lazy import there._toolset.pykeepsRestateContextRunToolSetand no longer touchespydantic_ai.mcp. The MCP code itself is moved as-is, no behaviour change.RestateMCPToolsetwas never exported fromrestate.ext.pydantic, so nothing public moves.One thing I noticed while testing: the repo's own
uv sync --all-extrasenvironment hasmcpbut notfastmcp(pydantic-ai-slim 2.15 needs both forpydantic_ai.mcp), so onmainimport restate.ext.pydanticfails there as well.Testing
Added
tests/ext_pydantic.py:test_import_without_mcpimportsrestate.ext.pydanticin a subprocess withmcp/fastmcpblockedtest_agent_with_function_tools_without_mcpbuilds aRestateAgentwith a function tool in the same setup and checks the tool is still wrapped inRestateContextRunToolSettest_agent_wraps_mcp_toolsetschecks anMCPToolsetis still wrapped inRestateMCPToolset(skipped whenpydantic_ai.mcpcan't be imported)The first two fail on
mainwith theImportErrorfrom the issue and pass with the change. I ran the third withpydantic-ai-slim[mcp]==2.15.0installed, and it passes. Because the dev environment doesn't havefastmcpyet,test_agent_wraps_mcp_toolsetswill be skipped in CI for now. Happy to addfastmcpto the dev deps in a follow-up so that path gets covered.I also checked the exact scenario from the issue in a clean venv with only
pydantic-ai-slim==2.15.0:import restate.ext.pydanticfails onmainand works on this branch.All the
just verifysteps pass locally (ruff format/check, pyright and mypy on python/, examples/, tests/, test-services/, pluspytest tests/*: 42 passed, 1 skipped).