Skip to content

fix: handle boolean JSON Schema nodes in JsonSchemaTransformer#4989

Open
Dharit13 wants to merge 1 commit intopydantic:mainfrom
Dharit13:fix/boolean-json-schema-nodes
Open

fix: handle boolean JSON Schema nodes in JsonSchemaTransformer#4989
Dharit13 wants to merge 1 commit intopydantic:mainfrom
Dharit13:fix/boolean-json-schema-nodes

Conversation

@Dharit13
Copy link
Copy Markdown

@Dharit13 Dharit13 commented Apr 5, 2026

Summary

JsonSchemaTransformer.walk() crashes with AttributeError: 'bool' object has no attribute 'get' when a JSON Schema contains boolean schema nodes (e.g. "value": true). Boolean schemas are valid per the JSON Schema spec (true is equivalent to {}, false is equivalent to {"not": {}}), but the walker unconditionally called .get() on every child node assuming it was a dict.

This PR treats boolean schema nodes as terminal leaves in the internal walker so they pass through untouched, and adds a parametrized regression test covering both True and False.

Pre-Review Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy.
  • Linting and type checking pass per make format and make typecheck.
  • PR title is fit for the release changelog.

Pre-Merge Checklist

  • New tests for any fix or new behavior, maintaining 100% coverage.
  • Updated documentation for new features and behaviors, including docstrings for API docs.

Made with Cursor

@github-actions github-actions bot added size: S Small PR (≤100 weighted lines) bug Report that something isn't working, or PR implementing a fix labels Apr 5, 2026
@Dharit13 Dharit13 force-pushed the fix/boolean-json-schema-nodes branch from 495c662 to 6f83a4b Compare April 5, 2026 16:27
Boolean JSON Schema nodes (`true`/`false`) are valid per the spec but
caused an `AttributeError` in the schema walker which unconditionally
called `.get()` on every child node. Treat booleans as terminal leaves
so the walker passes them through untouched.

Closes pydantic#4771

Made-with: Cursor
@Dharit13 Dharit13 force-pushed the fix/boolean-json-schema-nodes branch from 6f83a4b to 77e1b98 Compare April 5, 2026 16:38
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 new potential issue.

View 6 additional findings in Devin Review.

Open in Devin Review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚩 OpenAI transformer's result.update(self.defs.get(root_key) or {}) is fragile with widened defs type

The PR widens self.defs from dict[str, JsonSchema] to dict[str, _JsonSchemaNode] at pydantic_ai_slim/pydantic_ai/_json_schema.py:49. The OpenAIJsonSchemaTransformer.walk() at pydantic_ai_slim/pydantic_ai/profiles/openai.py:251 does result.update(self.defs.get(root_key) or {}). If root_key maps to True, True or {} evaluates to True, and result.update(True) raises TypeError: 'bool' object is not iterable. If it maps to False, False or {} silently produces {}, losing the false schema semantics. This is only reachable if a root-level $ref points to a boolean $def, which pydantic never generates — so it's a theoretical concern rather than a practical one. But the type system now claims to support it.

(Refers to line 49)

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

@Dharit13
Copy link
Copy Markdown
Author

Dharit13 commented Apr 5, 2026

Re: Devin Review comment on self.defs type widening

This was addressed in the latest push. self.defs is typed as dict[str, JsonSchema] (not _JsonSchemaNode), so .get(root_key) can only return dict[str, Any] | None, never a bool.

Boolean schema nodes only appear inline (as property values, array items, union members) and are handled as terminal leaves in _handle(). They never appear as $defs entries per the JSON Schema spec, so widening self.defs was unnecessary and has been reverted.

The result.update(self.defs.get(root_key) or {}) line in profiles/openai.py:251 is unaffected by this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Report that something isn't working, or PR implementing a fix size: S Small PR (≤100 weighted lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JsonSchemaTransformer crashes on valid boolean JSON Schema nodes

2 participants