From 229903825122797ec424f3bb2526acd45ebb8486 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 25 Jun 2025 23:37:56 +0000 Subject: [PATCH 1/2] Refactor `TypeChatJsonTranslator` to use public attributes for `type_name` and `schema_str`. --- python/src/typechat/_internal/translator.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/python/src/typechat/_internal/translator.py b/python/src/typechat/_internal/translator.py index 86b57126..ee077195 100644 --- a/python/src/typechat/_internal/translator.py +++ b/python/src/typechat/_internal/translator.py @@ -17,8 +17,8 @@ class TypeChatJsonTranslator(Generic[T]): model: TypeChatLanguageModel validator: TypeChatValidator[T] target_type: type[T] - _type_name: str - _schema_str: str + type_name: str + schema_str: str _max_repair_attempts = 1 def __init__( @@ -46,8 +46,8 @@ def __init__( error_text = "".join(f"\n- {error}" for error in conversion_result.errors) raise ValueError(f"Could not convert Python type to TypeScript schema: \n{error_text}") - self._type_name = conversion_result.typescript_type_reference - self._schema_str = conversion_result.typescript_schema_str + self.type_name = conversion_result.typescript_type_reference + self.schema_str = conversion_result.typescript_schema_str async def translate(self, input: str, *, prompt_preamble: str | list[PromptSection] | None = None) -> Result[T]: """ @@ -102,9 +102,9 @@ async def translate(self, input: str, *, prompt_preamble: str | list[PromptSecti def _create_request_prompt(self, intent: str) -> str: prompt = f""" -You are a service that translates user requests into JSON objects of type "{self._type_name}" according to the following TypeScript definitions: +You are a service that translates user requests into JSON objects of type "{self.type_name}" according to the following TypeScript definitions: ``` -{self._schema_str} +{self.schema_str} ``` The following is a user request: ''' From da2b39f103b99e8838112a112d9ce912f7e667c3 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 25 Jun 2025 23:42:03 +0000 Subject: [PATCH 2/2] Update samples. --- python/examples/healthData/translator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/examples/healthData/translator.py b/python/examples/healthData/translator.py index d9c1c46b..fa5749f2 100644 --- a/python/examples/healthData/translator.py +++ b/python/examples/healthData/translator.py @@ -41,9 +41,9 @@ def _create_request_prompt(self, intent: str) -> str: now = datetime.now() prompt = F""" -user: You are a service that translates user requests into JSON objects of type "{self._type_name}" according to the following TypeScript definitions: +user: You are a service that translates user requests into JSON objects of type "{self.type_name}" according to the following TypeScript definitions: ''' -{self._schema_str} +{self.schema_str} ''' user: