Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ def example_template(self) -> str:
return "```json\n{example}\n```"

def generate_response_example(self, example: dict) -> str:
return self.example_template.format(example=json.dumps(example))
return self.example_template.format(example=json.dumps(example, ensure_ascii=False))

@property
def schema(self) -> str:
return json.dumps(self.json_schema)
return json.dumps(self.json_schema, ensure_ascii=False)

def serialize_output(self, output: dict) -> str:
return json.dumps(output, ensure_ascii=False)
Expand Down Expand Up @@ -182,7 +182,7 @@ def __init__(self, data_type: type[BaseModel], **kwargs):

@property
def schema(self) -> str:
return json.dumps(self.data_type.model_json_schema())
return json.dumps(self.data_type.model_json_schema(), ensure_ascii=False)

@property
def task_instructions(self) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,47 @@ def test_structured_response_task_instructions():
assert "Baz!!" in recipe.task_instructions


class ThaiFoo(BaseModel):
province: str = Field(..., description="ชื่อจังหวัด")
region: str = Field(..., description="ภาค", json_schema_extra={"enum": ["ภาคเหนือ", "ภาคใต้"]})


class AsciiFoo(BaseModel):
province: str = Field(..., description="Province name")
region: str = Field(..., description="Region", json_schema_extra={"enum": ["North", "South"]})


@pytest.mark.parametrize(
"recipe",
[PydanticResponseRecipe(ThaiFoo), StructuredResponseRecipe(ThaiFoo.model_json_schema())],
ids=["pydantic", "structured"],
)
def test_json_recipe_task_instructions_keep_non_ascii_text(
recipe: PydanticResponseRecipe | StructuredResponseRecipe,
) -> None:
assert "ชื่อจังหวัด" in recipe.task_instructions
assert "ภาคเหนือ" in recipe.task_instructions
assert "\\u0e" not in recipe.task_instructions
assert json.loads(recipe.schema) == ThaiFoo.model_json_schema()


@pytest.mark.parametrize(
"recipe",
[PydanticResponseRecipe(AsciiFoo), StructuredResponseRecipe(AsciiFoo.model_json_schema())],
ids=["pydantic", "structured"],
)
def test_json_recipe_schema_unchanged_for_ascii(recipe: PydanticResponseRecipe | StructuredResponseRecipe) -> None:
assert recipe.schema == json.dumps(AsciiFoo.model_json_schema())


def test_structured_response_example_keeps_non_ascii_text() -> None:
recipe = StructuredResponseRecipe(ThaiFoo.model_json_schema())
example = {"province": "เชียงใหม่", "region": "ภาคเหนือ"}

assert "เชียงใหม่" in recipe.generate_response_example(example)
assert recipe.parse(recipe.generate_response_example(example)) == example


def test_pydantic_response_patched_case_single_lb():
recipe = PydanticResponseRecipe(Foo)
example = Foo(bar=Bar(baz=42))
Expand Down
Loading