Commit 8b914ac
fix: Add support for list-type JSON Schema fields in modeling.py (#37)
* fix: Add support for list-type JSON Schema fields in modeling.py
Problem:
- JSON Schema allows specifying multiple types using array notation: {"type": ["string", "number"]}
- This is valid per JSON Schema specification, and common in real-world schemas
- The mcpadapt modeling.py module failed with "unhashable type: 'list'" when processing such schemas
- Error occurs in get_field_type() when attempting to use a list as a dictionary key
Solution:
- Enhanced get_field_type() to properly handle list-type JSON Schema types
- Added special case to detect when json_type is a list
- Implemented conversion of list-type to Python Union types
- For single-item lists, extract and use the single type
- For multi-item lists, create a Union of all mapped types
- Preserves original behavior for all other schema types
This fix ensures compatibility with JSON Schema that use the array notation for
specifying multiple allowed types for a field, which is a common pattern in the
JSON Schema ecosystem. The fix is backwards compatible and follows the expected
behavior of properly converting JSON Schema types to their Python equivalents.
* test: Add dedicated test for JSON Schema list-type handling
This commit adds a comprehensive test suite for validating the fix for JSON Schema
array notation in type fields (e.g., "type": ["string", "number"]).
Key additions:
- New test file tests/test_modeling.py with multiple test scenarios:
- Direct test against modeling.py to verify handling of list-type JSON Schema fields
- Tests for array-type fields with multiple primitive types
- Specific tests for handling of null types in array notation
- Inspection utility to examine actual schema structure in MCP tools
The tests are designed to:
1. Verify the fix works correctly for all edge cases
2. Provide clear diagnostics when the bug is present
3. Demonstrate proper handling of various JSON Schema type patterns
4. Ensure consistent behavior with the existing anyOf implementation
The test handles both the "happy path" (with fix) and failure path (without fix),
making it valuable for preventing regressions. It also improves null type handling
to be consistent with how the codebase already handles nulls in anyOf constructs.
This testing approach validates that our implementation correctly supports the
JSON Schema specification, which allows multiple types to be specified either
via array notation or anyOf constructs.
* fix: Address PR #37 review feedback on JSON Schema list-type handling
- Extended JSON Schema array notation fix to langchain_adapter.py
- Added support for both array notation ("type": ["string", "number"])
and anyOf structures in LangChain adapter
- Reorganized tests per reviewer feedback:
* Moved simplified direct test to tests/utils/test_modeling.py
* Added E2E test to test_langchain_adapter.py
* Removed redundant test file
This commit ensures consistent handling of JSON Schema list-type fields
across all adapters and addresses all feedback from the PR review.
---------
Co-authored-by: Sakthi Kannan <saktr@amazon.com>1 parent 4deaddd commit 8b914ac
File tree
4 files changed
+137
-2
lines changed- src/mcpadapt
- utils
- tests
- utils
4 files changed
+137
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
87 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
88 | 119 | | |
89 | 120 | | |
90 | 121 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
157 | 157 | | |
158 | 158 | | |
159 | 159 | | |
160 | | - | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
161 | 177 | | |
162 | 178 | | |
163 | 179 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
10 | 36 | | |
11 | 37 | | |
12 | 38 | | |
| |||
112 | 138 | | |
113 | 139 | | |
114 | 140 | | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
115 | 166 | | |
116 | 167 | | |
117 | 168 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
0 commit comments