(client: OpenAI, respx_mock: MockRouter, monkeypatch: pytest.MonkeyPatch)
| 602 | |
| 603 | @pytest.mark.respx(base_url=base_url) |
| 604 | def test_parse_pydantic_tool(client: OpenAI, respx_mock: MockRouter, monkeypatch: pytest.MonkeyPatch) -> None: |
| 605 | class GetWeatherArgs(BaseModel): |
| 606 | city: str |
| 607 | country: str |
| 608 | units: Literal["c", "f"] = "c" |
| 609 | |
| 610 | completion = make_snapshot_request( |
| 611 | lambda c: c.chat.completions.parse( |
| 612 | model="gpt-4o-2024-08-06", |
| 613 | messages=[ |
| 614 | { |
| 615 | "role": "user", |
| 616 | "content": "What's the weather like in Edinburgh?", |
| 617 | }, |
| 618 | ], |
| 619 | tools=[ |
| 620 | openai.pydantic_function_tool(GetWeatherArgs), |
| 621 | ], |
| 622 | ), |
| 623 | content_snapshot=snapshot( |
| 624 | '{"id": "chatcmpl-ABfvx6Z4dchiW2nya1N8KMsHFrQRE", "object": "chat.completion", "created": 1727346165, "model": "gpt-4o-2024-08-06", "choices": [{"index": 0, "message": {"role": "assistant", "content": null, "tool_calls": [{"id": "call_Y6qJ7ofLgOrBnMD5WbVAeiRV", "type": "function", "function": {"name": "GetWeatherArgs", "arguments": "{\\"city\\":\\"Edinburgh\\",\\"country\\":\\"UK\\",\\"units\\":\\"c\\"}"}}], "refusal": null}, "logprobs": null, "finish_reason": "tool_calls"}], "usage": {"prompt_tokens": 76, "completion_tokens": 24, "total_tokens": 100, "completion_tokens_details": {"reasoning_tokens": 0}}, "system_fingerprint": "fp_e45dabd248"}' |
| 625 | ), |
| 626 | path="/chat/completions", |
| 627 | mock_client=client, |
| 628 | respx_mock=respx_mock, |
| 629 | ) |
| 630 | |
| 631 | assert print_obj(completion.choices, monkeypatch) == snapshot( |
| 632 | """\ |
| 633 | [ |
| 634 | ParsedChoice( |
| 635 | finish_reason='tool_calls', |
| 636 | index=0, |
| 637 | logprobs=None, |
| 638 | message=ParsedChatCompletionMessage( |
| 639 | annotations=None, |
| 640 | audio=None, |
| 641 | content=None, |
| 642 | function_call=None, |
| 643 | parsed=None, |
| 644 | refusal=None, |
| 645 | role='assistant', |
| 646 | tool_calls=[ |
| 647 | ParsedFunctionToolCall( |
| 648 | function=ParsedFunction( |
| 649 | arguments='{"city":"Edinburgh","country":"UK","units":"c"}', |
| 650 | name='GetWeatherArgs', |
| 651 | parsed_arguments=GetWeatherArgs(city='Edinburgh', country='UK', units='c') |
| 652 | ), |
| 653 | id='call_Y6qJ7ofLgOrBnMD5WbVAeiRV', |
| 654 | type='function' |
| 655 | ) |
| 656 | ] |
| 657 | ) |
| 658 | ) |
| 659 | ] |
| 660 | """ |
| 661 | ) |
nothing calls this directly
no test coverage detected