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

Function test_parse_pydantic_dataclass

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

Source from the content-addressed store, hash-verified

373@pytest.mark.respx(base_url=base_url)
374@pytest.mark.skipif(PYDANTIC_V1, reason="dataclasses only supported in v2")
375def test_parse_pydantic_dataclass(client: OpenAI, respx_mock: MockRouter, monkeypatch: pytest.MonkeyPatch) -> None:
376 from pydantic.dataclasses import dataclass
377
378 @dataclass
379 class CalendarEvent:
380 name: str
381 date: str
382 participants: List[str]
383
384 completion = make_snapshot_request(
385 lambda c: c.chat.completions.parse(
386 model="gpt-4o-2024-08-06",
387 messages=[
388 {"role": "system", "content": "Extract the event information."},
389 {"role": "user", "content": "Alice and Bob are going to a science fair on Friday."},
390 ],
391 response_format=CalendarEvent,
392 ),
393 content_snapshot=snapshot(
394 '{"id": "chatcmpl-ABfvqhz4uUUWsw8Ohw2Mp9B4sKKV8", "object": "chat.completion", "created": 1727346158, "model": "gpt-4o-2024-08-06", "choices": [{"index": 0, "message": {"role": "assistant", "content": "{\\"name\\":\\"Science Fair\\",\\"date\\":\\"Friday\\",\\"participants\\":[\\"Alice\\",\\"Bob\\"]}", "refusal": null}, "logprobs": null, "finish_reason": "stop"}], "usage": {"prompt_tokens": 92, "completion_tokens": 17, "total_tokens": 109, "completion_tokens_details": {"reasoning_tokens": 0}}, "system_fingerprint": "fp_7568d46099"}'
395 ),
396 path="/chat/completions",
397 mock_client=client,
398 respx_mock=respx_mock,
399 )
400
401 assert print_obj(completion, monkeypatch) == snapshot(
402 """\
403ParsedChatCompletion(
404 choices=[
405 ParsedChoice(
406 finish_reason='stop',
407 index=0,
408 logprobs=None,
409 message=ParsedChatCompletionMessage(
410 annotations=None,
411 audio=None,
412 content='{"name":"Science Fair","date":"Friday","participants":["Alice","Bob"]}',
413 function_call=None,
414 parsed=CalendarEvent(name='Science Fair', date='Friday', participants=['Alice', 'Bob']),
415 refusal=None,
416 role='assistant',
417 tool_calls=None
418 )
419 )
420 ],
421 created=1727346158,
422 id='chatcmpl-ABfvqhz4uUUWsw8Ohw2Mp9B4sKKV8',
423 model='gpt-4o-2024-08-06',
424 moderation=None,
425 object='chat.completion',
426 service_tier=None,
427 system_fingerprint='fp_7568d46099',
428 usage=CompletionUsage(
429 completion_tokens=17,
430 completion_tokens_details=CompletionTokensDetails(
431 accepted_prediction_tokens=None,
432 audio_tokens=None,

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