sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[PORT] strict_tools schema transform: recursive $ref crash, unclosed objects, and overwritten $ref targets

Open jimbrub opened this issue 1 month ago • 0 comments

Problem

When BedrockModel(strict_tools=True) is set, ensure_strict_json_schema in strands/models/_strict_schema.py is meant to add additionalProperties: false to every object in a tool's input schema. It has four bugs. The TS port (#4181) fixed them, so the two SDKs now behave differently.

1. A recursive $ref with sibling keys loops forever (crash)

If a $ref has sibling keys, the target gets inlined and then processed again. When the target refers back to itself, this never stops and raises RecursionError. A Pydantic model with a field of its own type produces this kind of schema, for example a tree node whose child field has a description. With strict_tools=True, every request that uses such a tool crashes.

ensure_strict_json_schema({
    "type": "object",
    "properties": {"n": {"$ref": "#/$defs/Node", "description": "d"}},
    "$defs": {"Node": {"type": "object", "properties": {
        "child": {"$ref": "#/$defs/Node", "description": "c"}}}},
})
# RecursionError

2. Objects with a union type are left open

The check is schema.get("type") == "object", so "type": ["object", "null"] is skipped. That's the shape of an Optional[Model] field. Bedrock then rejects the schema because the object isn't closed.

ensure_strict_json_schema({"type": ["object", "null"], "properties": {"a": {"type": "string"}}})
# no additionalProperties added

3. Objects nested under a schema-valued additionalProperties are left open

The function never recurses into additionalProperties when its value is a schema, so an object inside it (for example, the values of dict[str, Model]) stays open.

ensure_strict_json_schema({"type": "object", "additionalProperties": {
    "type": "object", "properties": {"x": {"type": "string"}}}})
# inner object not closed

4. A $ref target's own additionalProperties gets overwritten

A node like {"$ref": ..., "type": "object", ...} gets additionalProperties: false before it's inlined. During the merge, the node's own keys take priority, so that false replaces whatever the target defined.

ensure_strict_json_schema({"type": "object",
    "properties": {"m": {"$ref": "#/$defs/M", "type": "object", "description": "d"}},
    "$defs": {"M": {"type": "object", "additionalProperties": {"type": "string"}}}})
# properties.m.additionalProperties == False  (target defined {"type": "string"})

Proposed fix

Port the logic from strands-ts/src/models/strict-schema.ts:

  • Keep track of the $refs currently being inlined on this path. If one comes up again, leave it unresolved and log a warning instead of recursing.
  • Treat a schema as an object if type is "object" or is a list that includes "object".
  • Recurse into additionalProperties when its value is a schema. The TS code walks children through keyword lists (map / list / single-value), which also keeps complexity down.
  • Don't add additionalProperties to a node that still has a $ref. The inline step handles it, so the target's value is kept.

Add a unit test for each case above.

Out of scope, optional follow-up: TS also logs a one-time warning per tool when a schema uses keywords that Bedrock strict mode rejects (minimum/maximum, length bounds, open additionalProperties). Python could match this, but it's a separate change.

Related

  • #2475 / #4181: the TS strictTools implementation, which includes these fixes

jimbrub avatar Oct 09 '26 17:10 jimbrub