Skip to content

Commit b586896

Browse files
committed
Assert on generated input spec instead of json refs
1 parent d473fdb commit b586896

File tree

1 file changed

+79
-5
lines changed

1 file changed

+79
-5
lines changed

tests/strands/tools/test_structured_output.py

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
from typing import Literal, Optional
32

43
import pytest
@@ -243,9 +242,53 @@ class Person(BaseModel):
243242
list_of_item_or_nullable: list[Optional[Address]]
244243

245244
tool_spec = convert_pydantic_to_tool_spec(Person)
246-
raw_json = json.dumps(tool_spec, indent=2)
247245

248-
assert "$ref" not in raw_json
246+
expected_spec = {
247+
"description": "Complete person information.",
248+
"inputSchema": {
249+
"json": {
250+
"description": "Complete person information.",
251+
"properties": {
252+
"list_of_item_or_nullable": {
253+
"items": {
254+
"anyOf": [
255+
{
256+
"properties": {"postal_code": {"type": ["string", "null"]}},
257+
"title": "Address",
258+
"type": "object",
259+
},
260+
{"type": "null"},
261+
]
262+
},
263+
"title": "List Of Item Or Nullable",
264+
"type": "array",
265+
},
266+
"list_of_items": {
267+
"items": {
268+
"properties": {"postal_code": {"type": ["string", "null"]}},
269+
"title": "Address",
270+
"type": "object",
271+
},
272+
"title": "List Of Items",
273+
"type": "array",
274+
},
275+
"list_of_items_nullable": {
276+
"items": {
277+
"properties": {"postal_code": {"type": ["string", "null"]}},
278+
"title": "Address",
279+
"type": "object",
280+
},
281+
"type": ["array", "null"],
282+
},
283+
},
284+
"required": ["list_of_items", "list_of_item_or_nullable"],
285+
"title": "Person",
286+
"type": "object",
287+
}
288+
},
289+
"name": "Person",
290+
}
291+
assert tool_spec == expected_spec
249292

250293

251294
def test_convert_pydantic_with_refs():
@@ -266,6 +309,37 @@ class Person(BaseModel):
266309
contact: Contact = Field(description="Contact methods")
267310

268311
tool_spec = convert_pydantic_to_tool_spec(Person)
269-
raw_json = json.dumps(tool_spec, indent=2)
270312

271-
assert "$ref" not in raw_json
313+
expected_spec = {
314+
"description": "Complete person information.",
315+
"inputSchema": {
316+
"json": {
317+
"description": "Complete person information.",
318+
"properties": {
319+
"contact": {
320+
"description": "Contact methods",
321+
"properties": {
322+
"address": {
323+
"properties": {
324+
"city": {"title": "City", "type": "string"},
325+
"country": {"title": "Country", "type": "string"},
326+
"postal_code": {"type": ["string", "null"]},
327+
"street": {"title": "Street", "type": "string"},
328+
},
329+
"required": ["street", "city", "country"],
330+
"title": "Address",
331+
"type": "object",
332+
}
333+
},
334+
"required": ["address"],
335+
"type": "object",
336+
}
337+
},
338+
"required": ["contact"],
339+
"title": "Person",
340+
"type": "object",
341+
}
342+
},
343+
"name": "Person",
344+
}
345+
assert tool_spec == expected_spec

0 commit comments

Comments
 (0)