chore: import upstream snapshot with attribution
Ruff / Ruff (push) Has been cancelled
Test / Core Tests (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.10) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.11) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.12) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.13) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.9) (push) Has been cancelled
Test / Full Coverage (Python 3.11) (push) Has been cancelled
Test / Core Provider Tests (OpenAI) (push) Has been cancelled
Test / Core Provider Tests (Anthropic) (push) Has been cancelled
Test / Core Provider Tests (Google) (push) Has been cancelled
Test / Core Provider Tests (Other) (push) Has been cancelled
Test / Anthropic Tests (push) Has been cancelled
Test / Gemini Tests (push) Has been cancelled
Test / Google GenAI Tests (push) Has been cancelled
Test / Vertex AI Tests (push) Has been cancelled
Test / OpenAI Tests (push) Has been cancelled
Test / Writer Tests (push) Has been cancelled
Test / Auto Client Tests (push) Has been cancelled
ty / type-check (push) Has been cancelled
Ruff / Ruff (push) Has been cancelled
Test / Core Tests (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.10) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.11) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.12) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.13) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.9) (push) Has been cancelled
Test / Full Coverage (Python 3.11) (push) Has been cancelled
Test / Core Provider Tests (OpenAI) (push) Has been cancelled
Test / Core Provider Tests (Anthropic) (push) Has been cancelled
Test / Core Provider Tests (Google) (push) Has been cancelled
Test / Core Provider Tests (Other) (push) Has been cancelled
Test / Anthropic Tests (push) Has been cancelled
Test / Gemini Tests (push) Has been cancelled
Test / Google GenAI Tests (push) Has been cancelled
Test / Vertex AI Tests (push) Has been cancelled
Test / OpenAI Tests (push) Has been cancelled
Test / Writer Tests (push) Has been cancelled
Test / Auto Client Tests (push) Has been cancelled
ty / type-check (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
from typing import Any
|
||||
|
||||
from pydantic import BaseModel, create_model, Field
|
||||
from instructor import openai_schema
|
||||
|
||||
|
||||
def test_dynamic_model_creation_with_field_description():
|
||||
"""
|
||||
Test that dynamic model creation with Field(description) works correctly.
|
||||
This verifies the example in the documentation at docs/concepts/models.md.
|
||||
"""
|
||||
types: dict[str, type[Any]] = {
|
||||
"string": str,
|
||||
"integer": int,
|
||||
"email": str,
|
||||
}
|
||||
|
||||
mock_cursor = [
|
||||
("name", "string", "The name of the user."),
|
||||
("age", "integer", "The age of the user."),
|
||||
("email", "email", "The email of the user."),
|
||||
]
|
||||
|
||||
field_definitions: dict[str, Any] = {
|
||||
property_name: (types[property_type], Field(description=description))
|
||||
for property_name, property_type, description in mock_cursor
|
||||
}
|
||||
DynamicModel = create_model(
|
||||
"User",
|
||||
__base__=BaseModel,
|
||||
**field_definitions,
|
||||
)
|
||||
|
||||
schema = DynamicModel.model_json_schema()
|
||||
|
||||
assert schema["properties"]["name"]["description"] == "The name of the user."
|
||||
assert schema["properties"]["age"]["description"] == "The age of the user."
|
||||
assert schema["properties"]["email"]["description"] == "The email of the user."
|
||||
|
||||
assert "default" not in schema["properties"]["name"]
|
||||
assert "default" not in schema["properties"]["age"]
|
||||
assert "default" not in schema["properties"]["email"]
|
||||
|
||||
OpenAISchemaModel = openai_schema(DynamicModel)
|
||||
openai_schema_json = OpenAISchemaModel.model_json_schema()
|
||||
|
||||
assert (
|
||||
openai_schema_json["properties"]["name"]["description"]
|
||||
== "The name of the user."
|
||||
)
|
||||
assert (
|
||||
openai_schema_json["properties"]["age"]["description"] == "The age of the user."
|
||||
)
|
||||
assert (
|
||||
openai_schema_json["properties"]["email"]["description"]
|
||||
== "The email of the user."
|
||||
)
|
||||
Reference in New Issue
Block a user