From 62da30f3034cbe81046c333cf9390964223300f0 Mon Sep 17 00:00:00 2001 From: Good Samaritan <309353306+GoodSamaritan-bot-by-EG@users.noreply.github.com> Date: Fri, 16 Oct 2026 08:54:42 +0800 Subject: [PATCH] Fix #454: Generate JSON wire schemas for Python runtime type --- .../ts_conversion/python_type_to_ts_nodes.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/python/src/typechat/_internal/ts_conversion/python_type_to_ts_nodes.py b/python/src/typechat/_internal/ts_conversion/python_type_to_ts_nodes.py index ae64f6b2..6175afac 100644 --- a/python/src/typechat/_internal/ts_conversion/python_type_to_ts_nodes.py +++ b/python/src/typechat/_internal/ts_conversion/python_type_to_ts_nodes.py @@ -2,10 +2,13 @@ import collections.abc from collections import OrderedDict +import datetime +import decimal import inspect import sys import typing import typing_extensions +import uuid from dataclasses import MISSING, Field, dataclass from types import NoneType, UnionType from typing_extensions import ( @@ -236,6 +239,31 @@ def convert_to_type_node(py_type: object) -> TypeNode: if py_type is Self: return ThisTypeReferenceNode + # Standard library types supported by Pydantic + if py_type is datetime.datetime: + return Annotated( + StringTypeReferenceNode, + "ISO 8601 date-time string (e.g., '2022-08-20T08:30:00.000Z')" + ) + if py_type is datetime.date: + return Annotated(StringTypeReferenceNode, "ISO 8601 date string (e.g., '2022-08-20')") + if py_type is datetime.time: + return Annotated(StringTypeReferenceNode, "ISO 8601 time string (e.g., '08:30:00')") + if py_type is datetime.timedelta: + return Annotated(StringTypeReferenceNode, "ISO 8601 duration string (e.g., 'PT1H')") + if py_type is uuid.UUID: + return Annotated( + StringTypeReferenceNode, + "UUID string (e.g., '12345678-1234-5678-1234-567812345678')" + ) + if py_type is decimal.Decimal: + return Annotated(StringTypeReferenceNode, "Decimal number string (e.g., '12.50')") + if py_type is bytes: + return Annotated(StringTypeReferenceNode, "Base64 encoded bytes or UTF-8 string") + + if isinstance(py_type, Annotatedish): + return convert_to_type_node(py_type.__origin__) + # TODO: consider handling bare 'tuple' (and list, etc.) # https://docs.python.org/3/library/typing.html#annotating-tuples # Using plain tuple as an annotation is equivalent to using tuple[Any, ...]: