Skip to content

Commit fa6bbdf

Browse files
committed
more updates for perf
1 parent f72ef41 commit fa6bbdf

File tree

4 files changed

+35
-31
lines changed

4 files changed

+35
-31
lines changed

guardrails/async_guard.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -574,13 +574,13 @@ async def _stream_server_call(
574574
validated_output=validated_output,
575575
validation_passed=(validation_output.validation_passed is True),
576576
)
577-
if validation_output:
578-
guard_history = self._api_client.get_history(
579-
self.name, validation_output.call_id
580-
)
581-
self.history.extend(
582-
[Call.from_interface(call) for call in guard_history]
583-
)
577+
# if validation_output:
578+
# guard_history = self._api_client.get_history(
579+
# self.name, validation_output.call_id
580+
# )
581+
# self.history.extend(
582+
# [Call.from_interface(call) for call in guard_history]
583+
# )
584584
else:
585585
raise ValueError("AsyncGuard does not have an api client!")
586586

guardrails/run/async_stream_runner.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ async def async_step(
148148
_ = self.is_last_chunk(chunk, api)
149149

150150
fragment += chunk_text
151+
151152
results = await validator_service.async_partial_validate(
152153
chunk_text,
153154
self.metadata,
@@ -157,7 +158,8 @@ async def async_step(
157158
"$",
158159
True,
159160
)
160-
validators = self.validation_map["$"] or []
161+
validators = self.validation_map.get("$", [])
162+
161163
# collect the result validated_chunk into validation progress
162164
# per validator
163165
for result in results:
@@ -210,7 +212,7 @@ async def async_step(
210212
validation_progress[validator_log.validator_name] += chunk
211213
# if there is an entry for every validator
212214
# run a merge and emit a validation outcome
213-
if len(validation_progress) == len(validators):
215+
if len(validation_progress) == len(validators) or len(validators) == 0:
214216
if refrain_triggered:
215217
current = ""
216218
else:

guardrails/telemetry/guard_tracing.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,17 @@ def trace_stream_guard(
145145
res = next(result) # type: ignore
146146
# FIXME: This should only be called once;
147147
# Accumulate the validated output and call at the end
148-
if not guard_span.is_recording():
149-
# Assuming you have a tracer instance
150-
tracer = get_tracer(__name__)
151-
# Create a new span and link it to the previous span
152-
with tracer.start_as_current_span(
153-
"new_guard_span", # type: ignore
154-
links=[Link(guard_span.get_span_context())],
155-
) as new_span:
156-
guard_span = new_span
157-
add_guard_attributes(guard_span, history, res)
158-
add_user_attributes(guard_span)
148+
# if not guard_span.is_recording():
149+
# # Assuming you have a tracer instance
150+
# tracer = get_tracer(__name__)
151+
# # Create a new span and link it to the previous span
152+
# with tracer.start_as_current_span(
153+
# "new_guard_span", # type: ignore
154+
# links=[Link(guard_span.get_span_context())],
155+
# ) as new_span:
156+
# guard_span = new_span
157+
# add_guard_attributes(guard_span, history, res)
158+
# add_user_attributes(guard_span)
159159
yield res
160160
except StopIteration:
161161
next_exists = False
@@ -209,18 +209,18 @@ async def trace_async_stream_guard(
209209
while next_exists:
210210
try:
211211
res = await anext(result) # type: ignore
212-
if not guard_span.is_recording():
212+
# if not guard_span.is_recording():
213213
# Assuming you have a tracer instance
214-
tracer = get_tracer(__name__)
215-
# Create a new span and link it to the previous span
216-
with tracer.start_as_current_span(
217-
"new_guard_span", # type: ignore
218-
links=[Link(guard_span.get_span_context())],
219-
) as new_span:
220-
guard_span = new_span
221-
222-
add_guard_attributes(guard_span, history, res)
223-
add_user_attributes(guard_span)
214+
# tracer = get_tracer(__name__)
215+
# # Create a new span and link it to the previous span
216+
# with tracer.start_as_current_span(
217+
# "new_guard_span", # type: ignore
218+
# links=[Link(guard_span.get_span_context())],
219+
# ) as new_span:
220+
# guard_span = new_span
221+
222+
# add_guard_attributes(guard_span, history, res)
223+
# add_user_attributes(guard_span)
224224
yield res
225225
except StopIteration:
226226
next_exists = False

guardrails/validator_service/validator_service_base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ def run_validator(
169169

170170
# requires at least 2 validators
171171
def multi_merge(self, original: str, new_values: list[str]) -> Optional[str]:
172+
if len(new_values) == 0:
173+
return original
172174
current = new_values.pop()
173175
while len(new_values) > 0:
174176
nextval = new_values.pop()

0 commit comments

Comments
 (0)