|
36 | 36 | UsageContent, |
37 | 37 | UsageDetails, |
38 | 38 | ai_function, |
| 39 | + prepare_function_call_results, |
39 | 40 | ) |
40 | 41 | from agent_framework.exceptions import AdditionItemMismatch, ContentError |
41 | 42 |
|
@@ -1965,3 +1966,75 @@ def test_text_content_with_multiple_annotations_serialization(): |
1965 | 1966 | assert reconstructed.annotations[0].title == "Citation 1" |
1966 | 1967 | assert reconstructed.annotations[1].title == "Citation 2" |
1967 | 1968 | assert all(isinstance(ann.annotated_regions[0], TextSpanRegion) for ann in reconstructed.annotations) |
| 1969 | + |
| 1970 | + |
| 1971 | +# region prepare_function_call_results with Pydantic models |
| 1972 | + |
| 1973 | + |
| 1974 | +class WeatherResult(BaseModel): |
| 1975 | + """A Pydantic model for testing.""" |
| 1976 | + |
| 1977 | + temperature: float |
| 1978 | + condition: str |
| 1979 | + |
| 1980 | + |
| 1981 | +class NestedModel(BaseModel): |
| 1982 | + """A Pydantic model with nested structure.""" |
| 1983 | + |
| 1984 | + name: str |
| 1985 | + weather: WeatherResult |
| 1986 | + |
| 1987 | + |
| 1988 | +def test_prepare_function_call_results_pydantic_model(): |
| 1989 | + """Test that Pydantic BaseModel subclasses are properly serialized using model_dump().""" |
| 1990 | + result = WeatherResult(temperature=22.5, condition="sunny") |
| 1991 | + json_result = prepare_function_call_results(result) |
| 1992 | + |
| 1993 | + # The result should be a valid JSON string |
| 1994 | + assert isinstance(json_result, str) |
| 1995 | + assert '"temperature": 22.5' in json_result or '"temperature":22.5' in json_result |
| 1996 | + assert '"condition": "sunny"' in json_result or '"condition":"sunny"' in json_result |
| 1997 | + |
| 1998 | + |
| 1999 | +def test_prepare_function_call_results_pydantic_model_in_list(): |
| 2000 | + """Test that lists containing Pydantic models are properly serialized.""" |
| 2001 | + results = [ |
| 2002 | + WeatherResult(temperature=20.0, condition="cloudy"), |
| 2003 | + WeatherResult(temperature=25.0, condition="sunny"), |
| 2004 | + ] |
| 2005 | + json_result = prepare_function_call_results(results) |
| 2006 | + |
| 2007 | + # The result should be a valid JSON string representing a list |
| 2008 | + assert isinstance(json_result, str) |
| 2009 | + assert json_result.startswith("[") |
| 2010 | + assert json_result.endswith("]") |
| 2011 | + assert "cloudy" in json_result |
| 2012 | + assert "sunny" in json_result |
| 2013 | + |
| 2014 | + |
| 2015 | +def test_prepare_function_call_results_pydantic_model_in_dict(): |
| 2016 | + """Test that dicts containing Pydantic models are properly serialized.""" |
| 2017 | + results = { |
| 2018 | + "current": WeatherResult(temperature=22.0, condition="partly cloudy"), |
| 2019 | + "forecast": WeatherResult(temperature=24.0, condition="sunny"), |
| 2020 | + } |
| 2021 | + json_result = prepare_function_call_results(results) |
| 2022 | + |
| 2023 | + # The result should be a valid JSON string representing a dict |
| 2024 | + assert isinstance(json_result, str) |
| 2025 | + assert "current" in json_result |
| 2026 | + assert "forecast" in json_result |
| 2027 | + assert "partly cloudy" in json_result |
| 2028 | + assert "sunny" in json_result |
| 2029 | + |
| 2030 | + |
| 2031 | +def test_prepare_function_call_results_nested_pydantic_model(): |
| 2032 | + """Test that nested Pydantic models are properly serialized.""" |
| 2033 | + result = NestedModel(name="Seattle", weather=WeatherResult(temperature=18.0, condition="rainy")) |
| 2034 | + json_result = prepare_function_call_results(result) |
| 2035 | + |
| 2036 | + # The result should be a valid JSON string |
| 2037 | + assert isinstance(json_result, str) |
| 2038 | + assert "Seattle" in json_result |
| 2039 | + assert "rainy" in json_result |
| 2040 | + assert "18.0" in json_result or "18" in json_result |
0 commit comments