MCPcopy
hub / github.com/openai/openai-python / test_parse_pydantic_model

Function test_parse_pydantic_model

tests/lib/chat/test_completions.py:97–163  ·  view source on GitHub ↗
(client: OpenAI, respx_mock: MockRouter, monkeypatch: pytest.MonkeyPatch)

Source from the content-addressed store, hash-verified

95
96@pytest.mark.respx(base_url=base_url)
97def test_parse_pydantic_model(client: OpenAI, respx_mock: MockRouter, monkeypatch: pytest.MonkeyPatch) -> None:
98 class Location(BaseModel):
99 city: str
100 temperature: float
101 units: Literal["c", "f"]
102
103 completion = make_snapshot_request(
104 lambda c: c.chat.completions.parse(
105 model="gpt-4o-2024-08-06",
106 messages=[
107 {
108 "role": "user",
109 "content": "What's the weather like in SF?",
110 },
111 ],
112 response_format=Location,
113 ),
114 content_snapshot=snapshot(
115 '{"id": "chatcmpl-ABfvbtVnTu5DeC4EFnRYj8mtfOM99", "object": "chat.completion", "created": 1727346143, "model": "gpt-4o-2024-08-06", "choices": [{"index": 0, "message": {"role": "assistant", "content": "{\\"city\\":\\"San Francisco\\",\\"temperature\\":65,\\"units\\":\\"f\\"}", "refusal": null}, "logprobs": null, "finish_reason": "stop"}], "usage": {"prompt_tokens": 79, "completion_tokens": 14, "total_tokens": 93, "completion_tokens_details": {"reasoning_tokens": 0}}, "system_fingerprint": "fp_5050236cbd"}'
116 ),
117 path="/chat/completions",
118 mock_client=client,
119 respx_mock=respx_mock,
120 )
121
122 assert print_obj(completion, monkeypatch) == snapshot(
123 """\
124ParsedChatCompletion(
125 choices=[
126 ParsedChoice(
127 finish_reason='stop',
128 index=0,
129 logprobs=None,
130 message=ParsedChatCompletionMessage(
131 annotations=None,
132 audio=None,
133 content='{"city":"San Francisco","temperature":65,"units":"f"}',
134 function_call=None,
135 parsed=Location(city='San Francisco', temperature=65.0, units='f'),
136 refusal=None,
137 role='assistant',
138 tool_calls=None
139 )
140 )
141 ],
142 created=1727346143,
143 id='chatcmpl-ABfvbtVnTu5DeC4EFnRYj8mtfOM99',
144 model='gpt-4o-2024-08-06',
145 moderation=None,
146 object='chat.completion',
147 service_tier=None,
148 system_fingerprint='fp_5050236cbd',
149 usage=CompletionUsage(
150 completion_tokens=14,
151 completion_tokens_details=CompletionTokensDetails(
152 accepted_prediction_tokens=None,
153 audio_tokens=None,
154 reasoning_tokens=0,

Callers

nothing calls this directly

Calls 3

make_snapshot_requestFunction · 0.85
print_objFunction · 0.85
parseMethod · 0.45

Tested by

no test coverage detected