(client: OpenAI, respx_mock: MockRouter, monkeypatch: pytest.MonkeyPatch)
| 839 | |
| 840 | @pytest.mark.respx(base_url=base_url) |
| 841 | def test_parse_pydantic_raw_response(client: OpenAI, respx_mock: MockRouter, monkeypatch: pytest.MonkeyPatch) -> None: |
| 842 | class Location(BaseModel): |
| 843 | city: str |
| 844 | temperature: float |
| 845 | units: Literal["c", "f"] |
| 846 | |
| 847 | response = make_snapshot_request( |
| 848 | lambda c: c.chat.completions.with_raw_response.parse( |
| 849 | model="gpt-4o-2024-08-06", |
| 850 | messages=[ |
| 851 | { |
| 852 | "role": "user", |
| 853 | "content": "What's the weather like in SF?", |
| 854 | }, |
| 855 | ], |
| 856 | response_format=Location, |
| 857 | ), |
| 858 | content_snapshot=snapshot( |
| 859 | '{"id": "chatcmpl-ABrDYCa8W1w66eUxKDO8TQF1m6trT", "object": "chat.completion", "created": 1727389540, "model": "gpt-4o-2024-08-06", "choices": [{"index": 0, "message": {"role": "assistant", "content": "{\\"city\\":\\"San Francisco\\",\\"temperature\\":58,\\"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"}' |
| 860 | ), |
| 861 | path="/chat/completions", |
| 862 | mock_client=client, |
| 863 | respx_mock=respx_mock, |
| 864 | ) |
| 865 | assert response.http_request.headers.get("x-stainless-helper-method") == "chat.completions.parse" |
| 866 | |
| 867 | completion = response.parse() |
| 868 | message = completion.choices[0].message |
| 869 | assert message.parsed is not None |
| 870 | assert isinstance(message.parsed.city, str) |
| 871 | assert print_obj(completion, monkeypatch) == snapshot( |
| 872 | """\ |
| 873 | ParsedChatCompletion( |
| 874 | choices=[ |
| 875 | ParsedChoice( |
| 876 | finish_reason='stop', |
| 877 | index=0, |
| 878 | logprobs=None, |
| 879 | message=ParsedChatCompletionMessage( |
| 880 | annotations=None, |
| 881 | audio=None, |
| 882 | content='{"city":"San Francisco","temperature":58,"units":"f"}', |
| 883 | function_call=None, |
| 884 | parsed=Location(city='San Francisco', temperature=58.0, units='f'), |
| 885 | refusal=None, |
| 886 | role='assistant', |
| 887 | tool_calls=None |
| 888 | ) |
| 889 | ) |
| 890 | ], |
| 891 | created=1727389540, |
| 892 | id='chatcmpl-ABrDYCa8W1w66eUxKDO8TQF1m6trT', |
| 893 | model='gpt-4o-2024-08-06', |
| 894 | moderation=None, |
| 895 | object='chat.completion', |
| 896 | service_tier=None, |
| 897 | system_fingerprint='fp_5050236cbd', |
| 898 | usage=CompletionUsage( |
nothing calls this directly
no test coverage detected