@@ -14,6 +14,11 @@ class Responses:
14
14
"""Responses API with guardrails (sync)."""
15
15
16
16
def __init__ (self , client : GuardrailsBaseClient ) -> None :
17
+ """Initialize Responses resource.
18
+
19
+ Args:
20
+ client: GuardrailsBaseClient instance with configured guardrails.
21
+ """
17
22
self ._client = client
18
23
19
24
def create (
@@ -23,7 +28,7 @@ def create(
23
28
stream : bool = False ,
24
29
tools : list [dict ] | None = None ,
25
30
suppress_tripwire : bool = False ,
26
- ** kwargs
31
+ ** kwargs ,
27
32
):
28
33
"""Create response with guardrails (synchronous).
29
34
@@ -44,9 +49,7 @@ def create(
44
49
)
45
50
46
51
# Apply pre-flight modifications (PII masking, etc.)
47
- modified_input = self ._client ._apply_preflight_modifications (
48
- input , preflight_results
49
- )
52
+ modified_input = self ._client ._apply_preflight_modifications (input , preflight_results )
50
53
51
54
# Input guardrails and LLM call concurrently
52
55
with ThreadPoolExecutor (max_workers = 1 ) as executor :
@@ -83,14 +86,7 @@ def create(
83
86
suppress_tripwire = suppress_tripwire ,
84
87
)
85
88
86
- def parse (
87
- self ,
88
- input : list [dict [str , str ]],
89
- model : str ,
90
- text_format : type [BaseModel ],
91
- suppress_tripwire : bool = False ,
92
- ** kwargs
93
- ):
89
+ def parse (self , input : list [dict [str , str ]], model : str , text_format : type [BaseModel ], suppress_tripwire : bool = False , ** kwargs ):
94
90
"""Parse response with structured output and guardrails (synchronous)."""
95
91
latest_message , _ = self ._client ._extract_latest_user_message (input )
96
92
@@ -103,9 +99,7 @@ def parse(
103
99
)
104
100
105
101
# Apply pre-flight modifications (PII masking, etc.)
106
- modified_input = self ._client ._apply_preflight_modifications (
107
- input , preflight_results
108
- )
102
+ modified_input = self ._client ._apply_preflight_modifications (input , preflight_results )
109
103
110
104
# Input guardrails and LLM call concurrently
111
105
with ThreadPoolExecutor (max_workers = 1 ) as executor :
@@ -135,26 +129,30 @@ def parse(
135
129
def retrieve (self , response_id : str , suppress_tripwire : bool = False , ** kwargs ):
136
130
"""Retrieve response with output guardrail validation (synchronous)."""
137
131
# Get the response using the original OpenAI client
138
- response = self ._client ._resource_client .responses .retrieve (
139
- response_id , ** kwargs
140
- )
132
+ response = self ._client ._resource_client .responses .retrieve (response_id , ** kwargs )
141
133
142
134
# Run output guardrails on the retrieved content
143
135
output_text = response .output_text if hasattr (response , "output_text" ) else ""
144
- output_results = self ._client ._run_stage_guardrails (
145
- "output" , output_text , suppress_tripwire = suppress_tripwire
146
- )
136
+ output_results = self ._client ._run_stage_guardrails ("output" , output_text , suppress_tripwire = suppress_tripwire )
147
137
148
138
# Return wrapped response with guardrail results
149
139
return self ._client ._create_guardrails_response (
150
- response , [], [], output_results # preflight # input
140
+ response ,
141
+ [],
142
+ [],
143
+ output_results , # preflight # input
151
144
)
152
145
153
146
154
147
class AsyncResponses :
155
148
"""Responses API with guardrails (async)."""
156
149
157
150
def __init__ (self , client ):
151
+ """Initialize AsyncResponses resource.
152
+
153
+ Args:
154
+ client: GuardrailsBaseClient instance with configured guardrails.
155
+ """
158
156
self ._client = client
159
157
160
158
async def create (
@@ -164,7 +162,7 @@ async def create(
164
162
stream : bool = False ,
165
163
tools : list [dict ] | None = None ,
166
164
suppress_tripwire : bool = False ,
167
- ** kwargs
165
+ ** kwargs ,
168
166
) -> Any | AsyncIterator [Any ]:
169
167
"""Create response with guardrails."""
170
168
# Determine latest user message text when a list of messages is provided
@@ -182,9 +180,7 @@ async def create(
182
180
)
183
181
184
182
# Apply pre-flight modifications (PII masking, etc.)
185
- modified_input = self ._client ._apply_preflight_modifications (
186
- input , preflight_results
187
- )
183
+ modified_input = self ._client ._apply_preflight_modifications (input , preflight_results )
188
184
189
185
# Run input guardrails and LLM call in parallel
190
186
input_check = self ._client ._run_stage_guardrails (
@@ -220,13 +216,7 @@ async def create(
220
216
)
221
217
222
218
async def parse (
223
- self ,
224
- input : list [dict [str , str ]],
225
- model : str ,
226
- text_format : type [BaseModel ],
227
- stream : bool = False ,
228
- suppress_tripwire : bool = False ,
229
- ** kwargs
219
+ self , input : list [dict [str , str ]], model : str , text_format : type [BaseModel ], stream : bool = False , suppress_tripwire : bool = False , ** kwargs
230
220
) -> Any | AsyncIterator [Any ]:
231
221
"""Parse response with structured output and guardrails."""
232
222
latest_message , _ = self ._client ._extract_latest_user_message (input )
@@ -240,9 +230,7 @@ async def parse(
240
230
)
241
231
242
232
# Apply pre-flight modifications (PII masking, etc.)
243
- modified_input = self ._client ._apply_preflight_modifications (
244
- input , preflight_results
245
- )
233
+ modified_input = self ._client ._apply_preflight_modifications (input , preflight_results )
246
234
247
235
# Run input guardrails and LLM call in parallel
248
236
input_check = self ._client ._run_stage_guardrails (
@@ -277,22 +265,19 @@ async def parse(
277
265
suppress_tripwire = suppress_tripwire ,
278
266
)
279
267
280
- async def retrieve (
281
- self , response_id : str , suppress_tripwire : bool = False , ** kwargs
282
- ):
268
+ async def retrieve (self , response_id : str , suppress_tripwire : bool = False , ** kwargs ):
283
269
"""Retrieve response with output guardrail validation."""
284
270
# Get the response using the original OpenAI client
285
- response = await self ._client ._resource_client .responses .retrieve (
286
- response_id , ** kwargs
287
- )
271
+ response = await self ._client ._resource_client .responses .retrieve (response_id , ** kwargs )
288
272
289
273
# Run output guardrails on the retrieved content
290
274
output_text = response .output_text if hasattr (response , "output_text" ) else ""
291
- output_results = await self ._client ._run_stage_guardrails (
292
- "output" , output_text , suppress_tripwire = suppress_tripwire
293
- )
275
+ output_results = await self ._client ._run_stage_guardrails ("output" , output_text , suppress_tripwire = suppress_tripwire )
294
276
295
277
# Return wrapped response with guardrail results
296
278
return self ._client ._create_guardrails_response (
297
- response , [], [], output_results # preflight # input
279
+ response ,
280
+ [],
281
+ [],
282
+ output_results , # preflight # input
298
283
)
0 commit comments