Commit f58afb4
authored
fix(vertexai): fix "Object of type 'FieldInfo' is not JSON serializable" across all schema generation (#1317)
## Description
Fixes "TypeError: Object of type 'FieldInfo' is not JSON serializable"
that occurred when using with_structured_output(..., method="json_mode")
with Pydantic v2 models containing Field metadata.
The issue occurred because model_json_schema() was called without the
mode parameter, causing Pydantic v2 to use validation mode which can
include non-serializable FieldInfo objects in certain contexts. Adding
mode='serialization' tells Pydantic to use its serialization schema
generation mode, which properly handles FieldInfo objects.
Using mode='serialization' is also semantically correct since the schema
describes the model's output format (what Gemini should generate), not
input validation rules.
Changes:
- Add mode='serialization' to model_json_schema() in chat_models.py:2562
- Add unit test for Pydantic v2 FieldInfo serialization
- Add unit test for Pydantic v1 backward compatibility
- Verify both Pydantic v1 (schema() method) and v2 paths work correctly
Backward compatibility:
- Pydantic v1 models: Continue using schema() method (line 2560)
- Pydantic v2 models: Now use model_json_schema(mode='serialization')
- No public API changes, fully backward compatible
## Relevant issues
<!-- e.g. "Fixes #000" -->
## Type
<!-- Select the type of Pull Request -->
<!-- Keep only the necessary ones -->
🆕 New Feature
🐛 Bug Fix
🧹 Refactoring
📖 Documentation
🚄 Infrastructure
✅ Test
## Changes(optional)
<!-- List of changes -->
## Testing(optional)
<!-- Test procedure -->
<!-- Test result -->
## Note(optional)
<!-- Information about the errors fixed by PR -->
<!-- Remaining issue or something -->
<!-- Other information about PR -->
Signed-off-by: jitokim <pigberger70@gmail.com>1 parent 23aea19 commit f58afb4
File tree
4 files changed
+67
-4
lines changed- libs/vertexai
- langchain_google_vertexai
- tests/unit_tests
4 files changed
+67
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
| 51 | + | |
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2614 | 2614 | | |
2615 | 2615 | | |
2616 | 2616 | | |
2617 | | - | |
| 2617 | + | |
2618 | 2618 | | |
2619 | 2619 | | |
2620 | 2620 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
192 | 192 | | |
193 | 193 | | |
194 | 194 | | |
195 | | - | |
| 195 | + | |
196 | 196 | | |
197 | 197 | | |
198 | 198 | | |
| |||
211 | 211 | | |
212 | 212 | | |
213 | 213 | | |
214 | | - | |
| 214 | + | |
215 | 215 | | |
216 | 216 | | |
217 | 217 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1822 | 1822 | | |
1823 | 1823 | | |
1824 | 1824 | | |
| 1825 | + | |
| 1826 | + | |
| 1827 | + | |
| 1828 | + | |
| 1829 | + | |
| 1830 | + | |
| 1831 | + | |
| 1832 | + | |
| 1833 | + | |
| 1834 | + | |
| 1835 | + | |
| 1836 | + | |
| 1837 | + | |
| 1838 | + | |
| 1839 | + | |
| 1840 | + | |
| 1841 | + | |
| 1842 | + | |
| 1843 | + | |
| 1844 | + | |
| 1845 | + | |
| 1846 | + | |
| 1847 | + | |
| 1848 | + | |
| 1849 | + | |
| 1850 | + | |
| 1851 | + | |
| 1852 | + | |
| 1853 | + | |
| 1854 | + | |
| 1855 | + | |
| 1856 | + | |
| 1857 | + | |
| 1858 | + | |
| 1859 | + | |
| 1860 | + | |
| 1861 | + | |
| 1862 | + | |
| 1863 | + | |
| 1864 | + | |
| 1865 | + | |
| 1866 | + | |
| 1867 | + | |
| 1868 | + | |
| 1869 | + | |
| 1870 | + | |
| 1871 | + | |
| 1872 | + | |
| 1873 | + | |
| 1874 | + | |
| 1875 | + | |
| 1876 | + | |
| 1877 | + | |
| 1878 | + | |
| 1879 | + | |
| 1880 | + | |
| 1881 | + | |
| 1882 | + | |
| 1883 | + | |
| 1884 | + | |
| 1885 | + | |
| 1886 | + | |
| 1887 | + | |
1825 | 1888 | | |
1826 | 1889 | | |
1827 | 1890 | | |
| |||
0 commit comments