Skip to content

Commit c2c7191

Browse files
authored
Added missing doc strings / PR 9
Added missing doc strings
2 parents 7bb8bc0 + 0bbf02b commit c2c7191

File tree

3 files changed

+67
-74
lines changed

3 files changed

+67
-74
lines changed

pyproject.toml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,13 @@ select = [
8686
"D", # pydocstyle
8787
]
8888
isort = { combine-as-imports = true, known-first-party = ["guardrails"] }
89-
extend-ignore=[
90-
"D100", # Missing docstring in public module
91-
"D102", # Missing docstring in public method
92-
"D103", # Missing docstring in public function
93-
"D104", # Missing docstring in public package
94-
"D107", # Missing docstring in `__init__`
95-
]
9689

9790
[tool.ruff.lint.pydocstyle]
9891
convention = "google"
9992

10093
[tool.ruff.lint.extend-per-file-ignores]
101-
"tests/**" = ["E501"]
94+
"tests/**" = ["E501", "D100", "D103", "D104"]
95+
"examples/**" = ["D100", "D103", "D104"]
10296

10397
[tool.ruff.format]
10498
docstring-code-format = true

src/guardrails/resources/chat/chat.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,56 @@ class Chat:
1212
"""Chat completions with guardrails (sync)."""
1313

1414
def __init__(self, client: GuardrailsBaseClient) -> None:
15+
"""Initialize Chat resource.
16+
17+
Args:
18+
client: GuardrailsBaseClient instance with configured guardrails.
19+
"""
1520
self._client = client
1621

1722
@property
1823
def completions(self):
24+
"""Access chat completions API with guardrails.
25+
26+
Returns:
27+
ChatCompletions: Chat completions interface with guardrail support.
28+
"""
1929
return ChatCompletions(self._client)
2030

2131

2232
class AsyncChat:
2333
"""Chat completions with guardrails (async)."""
2434

2535
def __init__(self, client: GuardrailsBaseClient) -> None:
36+
"""Initialize AsyncChat resource.
37+
38+
Args:
39+
client: GuardrailsBaseClient instance with configured guardrails.
40+
"""
2641
self._client = client
2742

2843
@property
2944
def completions(self):
45+
"""Access async chat completions API with guardrails.
46+
47+
Returns:
48+
AsyncChatCompletions: Async chat completions with guardrail support.
49+
"""
3050
return AsyncChatCompletions(self._client)
3151

3252

3353
class ChatCompletions:
3454
"""Chat completions interface with guardrails (sync)."""
3555

3656
def __init__(self, client: GuardrailsBaseClient) -> None:
57+
"""Initialize ChatCompletions interface.
58+
59+
Args:
60+
client: GuardrailsBaseClient instance with configured guardrails.
61+
"""
3762
self._client = client
3863

39-
def create(
40-
self,
41-
messages: list[dict[str, str]],
42-
model: str,
43-
stream: bool = False,
44-
suppress_tripwire: bool = False,
45-
**kwargs
46-
):
64+
def create(self, messages: list[dict[str, str]], model: str, stream: bool = False, suppress_tripwire: bool = False, **kwargs):
4765
"""Create chat completion with guardrails (synchronous).
4866
4967
Runs preflight first, then executes input guardrails concurrently with the LLM call.
@@ -59,9 +77,7 @@ def create(
5977
)
6078

6179
# Apply pre-flight modifications (PII masking, etc.)
62-
modified_messages = self._client._apply_preflight_modifications(
63-
messages, preflight_results
64-
)
80+
modified_messages = self._client._apply_preflight_modifications(messages, preflight_results)
6581

6682
# Run input guardrails and LLM call concurrently using a thread for the LLM
6783
with ThreadPoolExecutor(max_workers=1) as executor:
@@ -102,15 +118,15 @@ class AsyncChatCompletions:
102118
"""Async chat completions interface with guardrails."""
103119

104120
def __init__(self, client):
121+
"""Initialize AsyncChatCompletions interface.
122+
123+
Args:
124+
client: GuardrailsBaseClient instance with configured guardrails.
125+
"""
105126
self._client = client
106127

107128
async def create(
108-
self,
109-
messages: list[dict[str, str]],
110-
model: str,
111-
stream: bool = False,
112-
suppress_tripwire: bool = False,
113-
**kwargs
129+
self, messages: list[dict[str, str]], model: str, stream: bool = False, suppress_tripwire: bool = False, **kwargs
114130
) -> Any | AsyncIterator[Any]:
115131
"""Create chat completion with guardrails."""
116132
latest_message, _ = self._client._extract_latest_user_message(messages)
@@ -124,9 +140,7 @@ async def create(
124140
)
125141

126142
# Apply pre-flight modifications (PII masking, etc.)
127-
modified_messages = self._client._apply_preflight_modifications(
128-
messages, preflight_results
129-
)
143+
modified_messages = self._client._apply_preflight_modifications(messages, preflight_results)
130144

131145
# Run input guardrails and LLM call concurrently for both streaming and non-streaming
132146
input_check = self._client._run_stage_guardrails(

src/guardrails/resources/responses/responses.py

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ class Responses:
1414
"""Responses API with guardrails (sync)."""
1515

1616
def __init__(self, client: GuardrailsBaseClient) -> None:
17+
"""Initialize Responses resource.
18+
19+
Args:
20+
client: GuardrailsBaseClient instance with configured guardrails.
21+
"""
1722
self._client = client
1823

1924
def create(
@@ -23,7 +28,7 @@ def create(
2328
stream: bool = False,
2429
tools: list[dict] | None = None,
2530
suppress_tripwire: bool = False,
26-
**kwargs
31+
**kwargs,
2732
):
2833
"""Create response with guardrails (synchronous).
2934
@@ -44,9 +49,7 @@ def create(
4449
)
4550

4651
# 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)
5053

5154
# Input guardrails and LLM call concurrently
5255
with ThreadPoolExecutor(max_workers=1) as executor:
@@ -83,14 +86,7 @@ def create(
8386
suppress_tripwire=suppress_tripwire,
8487
)
8588

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):
9490
"""Parse response with structured output and guardrails (synchronous)."""
9591
latest_message, _ = self._client._extract_latest_user_message(input)
9692

@@ -103,9 +99,7 @@ def parse(
10399
)
104100

105101
# 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)
109103

110104
# Input guardrails and LLM call concurrently
111105
with ThreadPoolExecutor(max_workers=1) as executor:
@@ -135,26 +129,30 @@ def parse(
135129
def retrieve(self, response_id: str, suppress_tripwire: bool = False, **kwargs):
136130
"""Retrieve response with output guardrail validation (synchronous)."""
137131
# 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)
141133

142134
# Run output guardrails on the retrieved content
143135
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)
147137

148138
# Return wrapped response with guardrail results
149139
return self._client._create_guardrails_response(
150-
response, [], [], output_results # preflight # input
140+
response,
141+
[],
142+
[],
143+
output_results, # preflight # input
151144
)
152145

153146

154147
class AsyncResponses:
155148
"""Responses API with guardrails (async)."""
156149

157150
def __init__(self, client):
151+
"""Initialize AsyncResponses resource.
152+
153+
Args:
154+
client: GuardrailsBaseClient instance with configured guardrails.
155+
"""
158156
self._client = client
159157

160158
async def create(
@@ -164,7 +162,7 @@ async def create(
164162
stream: bool = False,
165163
tools: list[dict] | None = None,
166164
suppress_tripwire: bool = False,
167-
**kwargs
165+
**kwargs,
168166
) -> Any | AsyncIterator[Any]:
169167
"""Create response with guardrails."""
170168
# Determine latest user message text when a list of messages is provided
@@ -182,9 +180,7 @@ async def create(
182180
)
183181

184182
# 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)
188184

189185
# Run input guardrails and LLM call in parallel
190186
input_check = self._client._run_stage_guardrails(
@@ -220,13 +216,7 @@ async def create(
220216
)
221217

222218
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
230220
) -> Any | AsyncIterator[Any]:
231221
"""Parse response with structured output and guardrails."""
232222
latest_message, _ = self._client._extract_latest_user_message(input)
@@ -240,9 +230,7 @@ async def parse(
240230
)
241231

242232
# 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)
246234

247235
# Run input guardrails and LLM call in parallel
248236
input_check = self._client._run_stage_guardrails(
@@ -277,22 +265,19 @@ async def parse(
277265
suppress_tripwire=suppress_tripwire,
278266
)
279267

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):
283269
"""Retrieve response with output guardrail validation."""
284270
# 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)
288272

289273
# Run output guardrails on the retrieved content
290274
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)
294276

295277
# Return wrapped response with guardrail results
296278
return self._client._create_guardrails_response(
297-
response, [], [], output_results # preflight # input
279+
response,
280+
[],
281+
[],
282+
output_results, # preflight # input
298283
)

0 commit comments

Comments
 (0)