Repository navigation
fix(pydantic): import restate.ext.pydantic without the pydantic-ai mcp extra - #257
pcbeingused333 wants to merge 3 commits into
Conversation
…p extra _toolset.py imported pydantic_ai.mcp at module level, so importing restate.ext.pydantic failed unless pydantic-ai-slim[mcp] was installed, although _agent.py already treats MCP support as optional. Move RestateMCPToolset into its own module that is only imported lazily.
|
All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
|
looks good thank you for your contribution. |
|
@pullfrog review |
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
Reviewed the change that moves the MCP toolset wrapper out of _toolset.py, so restate.ext.pydantic can be imported without the pydantic-ai-slim[mcp] extra.
- MCP wrapper moved to
_mcp_toolset.py:RestateMCPToolset,RestateMCPGetToolsContextRunResult,RestateMCPToolRunResultand their serdes are moved over unchanged, and the top-levelfrom pydantic_ai.mcp import MCPToolsetgoes with them. - Lazy import updated: the guarded
try/except ImportErrorinRestateAgent.__init__'sset_contextnow imports from._mcp_toolset.pydantic_ai/mcp.pyre-raises a missingmcp/fastmcpasImportError, so the guard catches it. - Regression test:
tests/pydantic_ai_without_mcp.pysetssys.modules['pydantic_ai.mcp'] = Nonein a subprocess and imports the extension. On the base commit this fails, because_toolset.pyimportspydantic_ai.mcpat module level.
What I checked:
main(0d06f2c) still has the module-level import, so the fix is still needed.- Nothing else on
mainimportsRestateMCPToolsetor the MCP serdes from_toolset. - GitHub reports the PR as mergeable against current
main. - On the PR head, the new test passes, and
ruff check,ruff format --checkandpyrightonpython/restate/ext/pydantic/all pass.
claude-opus-5-5 | 𝕏
# Conflicts: # python/restate/ext/pydantic/_toolset.py
|
|
||
|
|
||
| def test_import_without_pydantic_ai_mcp(): | ||
| """restate.ext.pydantic must be importable without the pydantic-ai ``mcp`` extra.""" |
There was a problem hiding this comment.
Lets remove this test please
| pytest.importorskip("pydantic_ai") | ||
|
|
||
|
|
||
| def test_import_without_pydantic_ai_mcp(): |
There was a problem hiding this comment.
please remove this entire test.
|
Merged with your authorship kept in #307 (the test left out, as the review asked). Thank you! |

Fixes #221.
python/restate/ext/pydantic/_toolset.pyimportedpydantic_ai.mcp.MCPToolsetat module level.restate.ext.pydanticimports that module from__init__and_agent, so the whole extension failed to import unlesspydantic-ai-slim[mcp](fastmcp) was installed, even though_agent.pyalready importsMCPToolsetandRestateMCPToolsetlazily inside atry/except ImportErrorand thepydantic_aiextra only pinspydantic-ai-slim.RestateMCPToolsetand its two serde dataclasses now live in a new_mcp_toolset.py, which is only imported from that guarded block._toolset.pykeepsRestateContextRunToolSetunchanged. Nothing else importedRestateMCPToolsetfrom_toolset.This pull request was prepared with the assistance of an AI coding agent (Claude Code) and submitted by Alex Castillo, who is responsible for its content.