Skip to content

Commit

Permalink
relax pydantic requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydanielfox committed Jan 30, 2025
1 parent 44c0004 commit 2a61357
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ dependencies = [
"anyio>=4.5",
"httpx>=0.27",
"httpx-sse>=0.4",
"pydantic>=2.10.1,<3.0.0",
"pydantic>=2.7.2,<3.0.0",
"starlette>=0.27",
"sse-starlette>=1.6.1",
"pydantic-settings>=2.6.1",
"uvicorn>=0.30",
"pydantic-settings>=2.5.2",
"uvicorn>=0.23.1",
]

[project.optional-dependencies]
Expand Down
43 changes: 42 additions & 1 deletion tests/server/fastmcp/test_func_metadata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Annotated

import annotated_types
import json
import pytest
from pydantic import BaseModel, Field

Expand Down Expand Up @@ -235,8 +236,48 @@ async def check_call(args):


def test_complex_function_json_schema():
"""Test JSON schema generation for complex function arguments.
Note: This test accepts two equivalent JSON Schema formats for models with defaults:
1. Pre-pydantic 2.7.2:
{
"$ref": "#/$defs/Model",
"default": {}
}
2. Pydantic 2.7.2+:
{
"allOf": [
{
"$ref": "#/$defs/Model"
}
],
"default": {}
}
Both formats are valid JSON Schema and represent the same validation rules.
The newer format using allOf is more correct according to the JSON Schema spec
as it properly composes the reference with additional properties.
This change in format does not affect runtime behavior since:
1. Both schemas validate the same way
2. The actual model classes and validation logic are unchanged
3. func_metadata uses model_validate/model_dump, not the schema directly
"""
meta = func_metadata(complex_arguments_fn)
assert meta.arg_model.model_json_schema() == {
actual_schema = meta.arg_model.model_json_schema()

# Create a copy of the actual schema to normalize
normalized_schema = actual_schema.copy()

# Normalize the my_model_a_with_default field to handle both pydantic formats
if 'allOf' in actual_schema['properties']['my_model_a_with_default']:
normalized_schema['properties']['my_model_a_with_default'] = {
'$ref': '#/$defs/SomeInputModelA',
'default': {}
}

assert normalized_schema == {
"$defs": {
"InnerModel": {
"properties": {"x": {"title": "X", "type": "integer"}},
Expand Down

0 comments on commit 2a61357

Please sign in to comment.