|
3 | 3 | import os
|
4 | 4 | import json
|
5 | 5 | from enum import Enum
|
6 |
| -from typing import Any, Callable, Optional |
| 6 | +from typing import Any, List, Callable, Optional |
7 | 7 | from typing_extensions import Literal, TypeVar
|
8 | 8 |
|
9 | 9 | import httpx
|
@@ -317,6 +317,63 @@ class Location(BaseModel):
|
317 | 317 | )
|
318 | 318 |
|
319 | 319 |
|
| 320 | +@pytest.mark.respx(base_url=base_url) |
| 321 | +@pytest.mark.skipif(not PYDANTIC_V2, reason="dataclasses only supported in v2") |
| 322 | +def test_parse_pydantic_dataclass(client: OpenAI, respx_mock: MockRouter, monkeypatch: pytest.MonkeyPatch) -> None: |
| 323 | + from pydantic.dataclasses import dataclass |
| 324 | + |
| 325 | + @dataclass |
| 326 | + class CalendarEvent: |
| 327 | + name: str |
| 328 | + date: str |
| 329 | + participants: List[str] |
| 330 | + |
| 331 | + completion = _make_snapshot_request( |
| 332 | + lambda c: c.beta.chat.completions.parse( |
| 333 | + model="gpt-4o-2024-08-06", |
| 334 | + messages=[ |
| 335 | + {"role": "system", "content": "Extract the event information."}, |
| 336 | + {"role": "user", "content": "Alice and Bob are going to a science fair on Friday."}, |
| 337 | + ], |
| 338 | + response_format=CalendarEvent, |
| 339 | + ), |
| 340 | + content_snapshot=snapshot( |
| 341 | + '{"id": "chatcmpl-9wdGqXkJJARAz7rOrLH5u5FBwLjF3", "object": "chat.completion", "created": 1723761008, "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": 32, "completion_tokens": 17, "total_tokens": 49}, "system_fingerprint": "fp_2a322c9ffc"}' |
| 342 | + ), |
| 343 | + mock_client=client, |
| 344 | + respx_mock=respx_mock, |
| 345 | + ) |
| 346 | + |
| 347 | + assert print_obj(completion, monkeypatch) == snapshot( |
| 348 | + """\ |
| 349 | +ParsedChatCompletion[CalendarEvent]( |
| 350 | + choices=[ |
| 351 | + ParsedChoice[CalendarEvent]( |
| 352 | + finish_reason='stop', |
| 353 | + index=0, |
| 354 | + logprobs=None, |
| 355 | + message=ParsedChatCompletionMessage[CalendarEvent]( |
| 356 | + content='{"name":"Science Fair","date":"Friday","participants":["Alice","Bob"]}', |
| 357 | + function_call=None, |
| 358 | + parsed=CalendarEvent(name='Science Fair', date='Friday', participants=['Alice', 'Bob']), |
| 359 | + refusal=None, |
| 360 | + role='assistant', |
| 361 | + tool_calls=[] |
| 362 | + ) |
| 363 | + ) |
| 364 | + ], |
| 365 | + created=1723761008, |
| 366 | + id='chatcmpl-9wdGqXkJJARAz7rOrLH5u5FBwLjF3', |
| 367 | + model='gpt-4o-2024-08-06', |
| 368 | + object='chat.completion', |
| 369 | + service_tier=None, |
| 370 | + system_fingerprint='fp_2a322c9ffc', |
| 371 | + usage=CompletionUsage(completion_tokens=17, prompt_tokens=32, total_tokens=49) |
| 372 | +) |
| 373 | +""" |
| 374 | + ) |
| 375 | + |
| 376 | + |
320 | 377 | @pytest.mark.respx(base_url=base_url)
|
321 | 378 | def test_pydantic_tool_model_all_types(client: OpenAI, respx_mock: MockRouter, monkeypatch: pytest.MonkeyPatch) -> None:
|
322 | 379 | completion = _make_snapshot_request(
|
|
0 commit comments