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

Function test_parse_pydantic_model_enum

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

Source from the content-addressed store, hash-verified

237
238@pytest.mark.respx(base_url=base_url)
239def test_parse_pydantic_model_enum(client: OpenAI, respx_mock: MockRouter, monkeypatch: pytest.MonkeyPatch) -> None:
240 class Color(Enum):
241 """The detected color"""
242
243 RED = "red"
244 BLUE = "blue"
245 GREEN = "green"
246
247 class ColorDetection(BaseModel):
248 color: Color
249 hex_color_code: str = Field(description="The hex color code of the detected color")
250
251 if PYDANTIC_V1:
252 ColorDetection.update_forward_refs(**locals()) # type: ignore
253
254 completion = make_snapshot_request(
255 lambda c: c.chat.completions.parse(
256 model="gpt-4o-2024-08-06",
257 messages=[
258 {"role": "user", "content": "What color is a Coke can?"},
259 ],
260 response_format=ColorDetection,
261 ),
262 content_snapshot=snapshot(
263 '{"id": "chatcmpl-ABfvjIatz0zrZu50gRbMtlp0asZpz", "object": "chat.completion", "created": 1727346151, "model": "gpt-4o-2024-08-06", "choices": [{"index": 0, "message": {"role": "assistant", "content": "{\\"color\\":\\"red\\",\\"hex_color_code\\":\\"#FF0000\\"}", "refusal": null}, "logprobs": null, "finish_reason": "stop"}], "usage": {"prompt_tokens": 109, "completion_tokens": 14, "total_tokens": 123, "completion_tokens_details": {"reasoning_tokens": 0}}, "system_fingerprint": "fp_5050236cbd"}'
264 ),
265 path="/chat/completions",
266 mock_client=client,
267 respx_mock=respx_mock,
268 )
269
270 assert print_obj(completion.choices[0], monkeypatch) == snapshot(
271 """\
272ParsedChoice(
273 finish_reason='stop',
274 index=0,
275 logprobs=None,
276 message=ParsedChatCompletionMessage(
277 annotations=None,
278 audio=None,
279 content='{"color":"red","hex_color_code":"#FF0000"}',
280 function_call=None,
281 parsed=ColorDetection(color=<Color.RED: 'red'>, hex_color_code='#FF0000'),
282 refusal=None,
283 role='assistant',
284 tool_calls=None
285 )
286)
287"""
288 )
289
290
291@pytest.mark.respx(base_url=base_url)

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