(client: OpenAI, respx_mock: MockRouter, monkeypatch: pytest.MonkeyPatch)
| 95 | |
| 96 | @pytest.mark.respx(base_url=base_url) |
| 97 | def test_parse_pydantic_model(client: OpenAI, respx_mock: MockRouter, monkeypatch: pytest.MonkeyPatch) -> None: |
| 98 | class Location(BaseModel): |
| 99 | city: str |
| 100 | temperature: float |
| 101 | units: Literal["c", "f"] |
| 102 | |
| 103 | completion = make_snapshot_request( |
| 104 | lambda c: c.chat.completions.parse( |
| 105 | model="gpt-4o-2024-08-06", |
| 106 | messages=[ |
| 107 | { |
| 108 | "role": "user", |
| 109 | "content": "What's the weather like in SF?", |
| 110 | }, |
| 111 | ], |
| 112 | response_format=Location, |
| 113 | ), |
| 114 | content_snapshot=snapshot( |
| 115 | '{"id": "chatcmpl-ABfvbtVnTu5DeC4EFnRYj8mtfOM99", "object": "chat.completion", "created": 1727346143, "model": "gpt-4o-2024-08-06", "choices": [{"index": 0, "message": {"role": "assistant", "content": "{\\"city\\":\\"San Francisco\\",\\"temperature\\":65,\\"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"}' |
| 116 | ), |
| 117 | path="/chat/completions", |
| 118 | mock_client=client, |
| 119 | respx_mock=respx_mock, |
| 120 | ) |
| 121 | |
| 122 | assert print_obj(completion, monkeypatch) == snapshot( |
| 123 | """\ |
| 124 | ParsedChatCompletion( |
| 125 | choices=[ |
| 126 | ParsedChoice( |
| 127 | finish_reason='stop', |
| 128 | index=0, |
| 129 | logprobs=None, |
| 130 | message=ParsedChatCompletionMessage( |
| 131 | annotations=None, |
| 132 | audio=None, |
| 133 | content='{"city":"San Francisco","temperature":65,"units":"f"}', |
| 134 | function_call=None, |
| 135 | parsed=Location(city='San Francisco', temperature=65.0, units='f'), |
| 136 | refusal=None, |
| 137 | role='assistant', |
| 138 | tool_calls=None |
| 139 | ) |
| 140 | ) |
| 141 | ], |
| 142 | created=1727346143, |
| 143 | id='chatcmpl-ABfvbtVnTu5DeC4EFnRYj8mtfOM99', |
| 144 | model='gpt-4o-2024-08-06', |
| 145 | moderation=None, |
| 146 | object='chat.completion', |
| 147 | service_tier=None, |
| 148 | system_fingerprint='fp_5050236cbd', |
| 149 | usage=CompletionUsage( |
| 150 | completion_tokens=14, |
| 151 | completion_tokens_details=CompletionTokensDetails( |
| 152 | accepted_prediction_tokens=None, |
| 153 | audio_tokens=None, |
| 154 | reasoning_tokens=0, |
nothing calls this directly
no test coverage detected