1
1
from __future__ import annotations
2
2
3
- import os
4
- import json
5
3
from enum import Enum
6
- from typing import Any , List , Callable , Optional , Awaitable
4
+ from typing import List , Optional
7
5
from typing_extensions import Literal , TypeVar
8
6
9
- import httpx
10
7
import pytest
11
8
from respx import MockRouter
12
9
from pydantic import Field , BaseModel
17
14
from openai ._utils import assert_signatures_in_sync
18
15
from openai ._compat import PYDANTIC_V2
19
16
20
- from ._utils import print_obj , get_snapshot_value
17
+ from .. utils import print_obj
21
18
from ...conftest import base_url
19
+ from ..snapshots import make_snapshot_request , make_async_snapshot_request
22
20
from ..schema_types .query import Query
23
21
24
22
_T = TypeVar ("_T" )
32
30
33
31
@pytest .mark .respx (base_url = base_url )
34
32
def test_parse_nothing (client : OpenAI , respx_mock : MockRouter , monkeypatch : pytest .MonkeyPatch ) -> None :
35
- completion = _make_snapshot_request (
33
+ completion = make_snapshot_request (
36
34
lambda c : c .chat .completions .parse (
37
35
model = "gpt-4o-2024-08-06" ,
38
36
messages = [
@@ -100,7 +98,7 @@ class Location(BaseModel):
100
98
temperature : float
101
99
units : Literal ["c" , "f" ]
102
100
103
- completion = _make_snapshot_request (
101
+ completion = make_snapshot_request (
104
102
lambda c : c .chat .completions .parse (
105
103
model = "gpt-4o-2024-08-06" ,
106
104
messages = [
@@ -170,7 +168,7 @@ class Location(BaseModel):
170
168
temperature : float
171
169
units : Optional [Literal ["c" , "f" ]] = None
172
170
173
- completion = _make_snapshot_request (
171
+ completion = make_snapshot_request (
174
172
lambda c : c .chat .completions .parse (
175
173
model = "gpt-4o-2024-08-06" ,
176
174
messages = [
@@ -247,7 +245,7 @@ class ColorDetection(BaseModel):
247
245
if not PYDANTIC_V2 :
248
246
ColorDetection .update_forward_refs (** locals ()) # type: ignore
249
247
250
- completion = _make_snapshot_request (
248
+ completion = make_snapshot_request (
251
249
lambda c : c .chat .completions .parse (
252
250
model = "gpt-4o-2024-08-06" ,
253
251
messages = [
@@ -292,7 +290,7 @@ class Location(BaseModel):
292
290
temperature : float
293
291
units : Literal ["c" , "f" ]
294
292
295
- completion = _make_snapshot_request (
293
+ completion = make_snapshot_request (
296
294
lambda c : c .chat .completions .parse (
297
295
model = "gpt-4o-2024-08-06" ,
298
296
messages = [
@@ -375,7 +373,7 @@ class CalendarEvent:
375
373
date : str
376
374
participants : List [str ]
377
375
378
- completion = _make_snapshot_request (
376
+ completion = make_snapshot_request (
379
377
lambda c : c .chat .completions .parse (
380
378
model = "gpt-4o-2024-08-06" ,
381
379
messages = [
@@ -436,7 +434,7 @@ class CalendarEvent:
436
434
437
435
@pytest .mark .respx (base_url = base_url )
438
436
def test_pydantic_tool_model_all_types (client : OpenAI , respx_mock : MockRouter , monkeypatch : pytest .MonkeyPatch ) -> None :
439
- completion = _make_snapshot_request (
437
+ completion = make_snapshot_request (
440
438
lambda c : c .chat .completions .parse (
441
439
model = "gpt-4o-2024-08-06" ,
442
440
messages = [
@@ -521,7 +519,7 @@ class Location(BaseModel):
521
519
units : Literal ["c" , "f" ]
522
520
523
521
with pytest .raises (openai .LengthFinishReasonError ):
524
- _make_snapshot_request (
522
+ make_snapshot_request (
525
523
lambda c : c .chat .completions .parse (
526
524
model = "gpt-4o-2024-08-06" ,
527
525
messages = [
@@ -548,7 +546,7 @@ class Location(BaseModel):
548
546
temperature : float
549
547
units : Literal ["c" , "f" ]
550
548
551
- completion = _make_snapshot_request (
549
+ completion = make_snapshot_request (
552
550
lambda c : c .chat .completions .parse (
553
551
model = "gpt-4o-2024-08-06" ,
554
552
messages = [
@@ -596,7 +594,7 @@ class GetWeatherArgs(BaseModel):
596
594
country : str
597
595
units : Literal ["c" , "f" ] = "c"
598
596
599
- completion = _make_snapshot_request (
597
+ completion = make_snapshot_request (
600
598
lambda c : c .chat .completions .parse (
601
599
model = "gpt-4o-2024-08-06" ,
602
600
messages = [
@@ -662,7 +660,7 @@ class GetStockPrice(BaseModel):
662
660
ticker : str
663
661
exchange : str
664
662
665
- completion = _make_snapshot_request (
663
+ completion = make_snapshot_request (
666
664
lambda c : c .chat .completions .parse (
667
665
model = "gpt-4o-2024-08-06" ,
668
666
messages = [
@@ -733,7 +731,7 @@ class GetStockPrice(BaseModel):
733
731
734
732
@pytest .mark .respx (base_url = base_url )
735
733
def test_parse_strict_tools (client : OpenAI , respx_mock : MockRouter , monkeypatch : pytest .MonkeyPatch ) -> None :
736
- completion = _make_snapshot_request (
734
+ completion = make_snapshot_request (
737
735
lambda c : c .chat .completions .parse (
738
736
model = "gpt-4o-2024-08-06" ,
739
737
messages = [
@@ -830,7 +828,7 @@ class Location(BaseModel):
830
828
temperature : float
831
829
units : Literal ["c" , "f" ]
832
830
833
- response = _make_snapshot_request (
831
+ response = make_snapshot_request (
834
832
lambda c : c .chat .completions .with_raw_response .parse (
835
833
model = "gpt-4o-2024-08-06" ,
836
834
messages = [
@@ -906,7 +904,7 @@ class Location(BaseModel):
906
904
temperature : float
907
905
units : Literal ["c" , "f" ]
908
906
909
- response = await _make_async_snapshot_request (
907
+ response = await make_async_snapshot_request (
910
908
lambda c : c .chat .completions .with_raw_response .parse (
911
909
model = "gpt-4o-2024-08-06" ,
912
910
messages = [
@@ -981,87 +979,3 @@ def test_parse_method_in_sync(sync: bool, client: OpenAI, async_client: AsyncOpe
981
979
checking_client .chat .completions .parse ,
982
980
exclude_params = {"response_format" , "stream" },
983
981
)
984
-
985
-
986
- def _make_snapshot_request (
987
- func : Callable [[OpenAI ], _T ],
988
- * ,
989
- content_snapshot : Any ,
990
- respx_mock : MockRouter ,
991
- mock_client : OpenAI ,
992
- ) -> _T :
993
- live = os .environ .get ("OPENAI_LIVE" ) == "1"
994
- if live :
995
-
996
- def _on_response (response : httpx .Response ) -> None :
997
- # update the content snapshot
998
- assert json .dumps (json .loads (response .read ())) == content_snapshot
999
-
1000
- respx_mock .stop ()
1001
-
1002
- client = OpenAI (
1003
- http_client = httpx .Client (
1004
- event_hooks = {
1005
- "response" : [_on_response ],
1006
- }
1007
- )
1008
- )
1009
- else :
1010
- respx_mock .post ("/chat/completions" ).mock (
1011
- return_value = httpx .Response (
1012
- 200 ,
1013
- content = get_snapshot_value (content_snapshot ),
1014
- headers = {"content-type" : "application/json" },
1015
- )
1016
- )
1017
-
1018
- client = mock_client
1019
-
1020
- result = func (client )
1021
-
1022
- if live :
1023
- client .close ()
1024
-
1025
- return result
1026
-
1027
-
1028
- async def _make_async_snapshot_request (
1029
- func : Callable [[AsyncOpenAI ], Awaitable [_T ]],
1030
- * ,
1031
- content_snapshot : Any ,
1032
- respx_mock : MockRouter ,
1033
- mock_client : AsyncOpenAI ,
1034
- ) -> _T :
1035
- live = os .environ .get ("OPENAI_LIVE" ) == "1"
1036
- if live :
1037
-
1038
- async def _on_response (response : httpx .Response ) -> None :
1039
- # update the content snapshot
1040
- assert json .dumps (json .loads (await response .aread ())) == content_snapshot
1041
-
1042
- respx_mock .stop ()
1043
-
1044
- client = AsyncOpenAI (
1045
- http_client = httpx .AsyncClient (
1046
- event_hooks = {
1047
- "response" : [_on_response ],
1048
- }
1049
- )
1050
- )
1051
- else :
1052
- respx_mock .post ("/chat/completions" ).mock (
1053
- return_value = httpx .Response (
1054
- 200 ,
1055
- content = get_snapshot_value (content_snapshot ),
1056
- headers = {"content-type" : "application/json" },
1057
- )
1058
- )
1059
-
1060
- client = mock_client
1061
-
1062
- result = await func (client )
1063
-
1064
- if live :
1065
- await client .close ()
1066
-
1067
- return result
0 commit comments