Skip to content

fix(python): return Failure when diagnostics are not JSON-serializable - #546

Open
Cai199999 wants to merge 2 commits into
microsoft:mainfrom
Cai199999:fix/python-nonjson-diagnostics
Open

Cai199999 wants to merge 2 commits into
microsoft:mainfrom
Cai199999:fix/python-nonjson-diagnostics

Conversation

@Cai199999

Copy link
Copy Markdown

Summary

TypeChatValidator.validate_object raises TypeError instead of
returning
Failure when a Pydantic validation diagnostic contains a value that is
not
JSON-serializable — for example bytes produced by a BeforeValidator.

Before:

TypeError: Object of type bytes is not JSON serializable

After:

Failure
Validation path `value` failed for value `b'bad'` because:
  Input should be a valid integer

The original rejected object is still preserved in Failure.value.
Diagnostics that are already JSON-serializable keep their previous
formatting.

Changes

- python/src/typechat/_internal/validator.py: in _handle_error, try
  json.dumps(input); on TypeError/ValueError, fall back to repr(input).
- python/tests/test_validator.py: regression test for the bytes diagnostic
  plus a valid-input control.

Tests

- pytest python/tests -q → 751 passed
- pyright 1.1.358 (strict) → 0 errors
- ruff and black --check on the changed files → clean

Fixes #544

Copilot AI balanced review requested due to automatic review settings November 26, 2026 07:35

Copilot AI left a comment

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.

🟡 Changes recommended

Both new tests fail on supported Python 3.11 because Pydantic rejects typing.TypedDict, so they must import it from typing_extensions before they can exercise validation or the diagnostic fallback.

1 open issue
What changed in this PR

This PR addresses #544 by making Python validation return Failure when Pydantic diagnostics contain non-JSON-serializable values, while preserving the original rejected input.

Changes:

  • Fall back to repr when diagnostic JSON serialization raises TypeError or ValueError.
  • Add regression coverage for bytes diagnostics and a valid-input control.
  • Preserve existing formatting for JSON-serializable diagnostics.
File Description
python/​tests/​test_validator.py Adds bytes-diagnostic regression and valid-input tests.
python/​src/​typechat/​_internal/​validator.py Adds fallback formatting for non-JSON-serializable diagnostic values.

Review effort: Balanced

Comment thread python/tests/test_validator.py Resolved
Copilot AI balanced review requested due to automatic review settings November 26, 2026 07:41
@Cai199999

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Copilot AI left a comment

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.

🟢 Approval recommended

No active review issues are reported for the fallback handling of non-JSON-serializable validation diagnostics.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants