@@ -26,59 +26,27 @@ def async_openai_client():
2626
2727@pytest .fixture (scope = "module" )
2828def vcr_config ():
29- def before_record_response (response ):
30- # Mask rate limit information and sensitive headers
31- headers = response ["headers" ]
32- sensitive_headers = {
33- "x-request-id" , "x-Request-id" , "X-Request-ID" ,
34- "openai-organization" , "openai-Organization" , "openai-ORGANIZATION" ,
35- "set-cookie" , "Set-Cookie" ,
36- "_cfuvid" , "__cf_bm" ,
37- "openai-processing-ms"
38- }
39-
40- for key in list (headers .keys ()):
41- # Case insensitive header matching
42- if any (sensitive .lower () == key .lower () for sensitive in sensitive_headers ):
43- headers [key ] = ["[FILTERED]" ]
44- elif key .lower ().startswith (("x-ratelimit-" , "openai-processing" )):
45- headers [key ] = ["0" ]
46-
47- # Keep response structure but mask actual values
48- if "body" in response and "string" in response ["body" ]:
49- try :
50- import json
51- import base64
52- decoded = json .loads (base64 .b64decode (response ["body" ]["string" ]))
53- if "data" in decoded and isinstance (decoded ["data" ], list ):
54- for item in decoded ["data" ]:
55- if "embedding" in item :
56- item ["embedding" ] = [0.1 ] * len (item ["embedding" ])
57- response ["body" ]["string" ] = base64 .b64encode (json .dumps (decoded ).encode ()).decode ()
58- except :
59- pass
60- return response
61-
6229 return {
6330 "filter_headers" : [
64- "authorization" ,
65- "api-key" ,
66- "user-agent" ,
67- "x-stainless-arch" ,
68- "x-stainless-async" ,
69- "x-stainless-lang" ,
70- "x-stainless-os" ,
71- "x-stainless-package-version" ,
72- "x-stainless-runtime" ,
73- "x-stainless-runtime-version" ,
74- "host" ,
75- "cf-ray" ,
31+ ("cookie" , "test_cookie" ),
32+ ("authorization" , "Bearer test_openai_api_key" ),
33+ ("openai-organization" , "test_openai_org_id" ),
34+ ("openai-project" , "test_openai_project_id" ),
7635 ],
77- "filter_query_parameters " : [ "api-version" ] ,
78- "before_record_response" : before_record_response ,
36+ "decode_compressed_response " : True ,
37+ "before_record_response" : scrub_response_headers ,
7938 }
8039
8140
8241@pytest .fixture (scope = "session" , autouse = True )
8342def instrument ():
8443 OpenAIInstrumentation ().instrument ()
44+
45+
46+ def scrub_response_headers (response ):
47+ """
48+ This scrubs sensitive response headers. Note they are case-sensitive!
49+ """
50+ response ["headers" ]["openai-organization" ] = "test_openai_org_id"
51+ response ["headers" ]["Set-Cookie" ] = "test_set_cookie"
52+ return response
0 commit comments