Skip to content

Bugfix/1221 #1226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions guardrails/classes/history/call_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,29 @@ def from_dict(cls, obj: Dict[str, Any]):
i_call_inputs = ICallInputs.from_dict(obj) or ICallInputs()

return cls.from_interface(i_call_inputs)

def __str__(self):
attributes = {}
for k, v in self.__dict__.items():
if not k.startswith("_"):
if k == "kwargs":
redacted_kwargs = {}
for sk, sv in v.items():
if (
"key" in sk.lower() or "token" in sk.lower()
) and isinstance(sv, str):
redaction_length = len(sv) - 4
stars = "*" * redaction_length
redacted_kwargs[sk] = f"{stars}{sv[-4:]}"
else:
redacted_kwargs[sk] = sv
attributes[k] = redacted_kwargs
else:
attributes[k] = v
class_name = self.__class__.__name__
return (
f"{class_name}(\n"
+ ",\
\n".join(f" {k}={v}" for k, v in attributes.items())
+ "\n)"
)
15 changes: 13 additions & 2 deletions guardrails/telemetry/guard_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Union,
)

from openinference.semconv.trace import SpanAttributes
from opentelemetry import context, trace
from opentelemetry.trace import StatusCode, Tracer, Span, Link, get_tracer

Expand Down Expand Up @@ -153,6 +154,9 @@ def trace_stream_guard(
guard_span = new_span
add_guard_attributes(guard_span, history, res)
add_user_attributes(guard_span)
new_span.set_attribute(
SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL"
)
yield res
except StopIteration:
next_exists = False
Expand All @@ -179,7 +183,9 @@ def trace_guard_execution(
guard_span.set_attribute("guardrails.version", GUARDRAILS_VERSION)
guard_span.set_attribute("type", "guardrails/guard")
guard_span.set_attribute("guard.name", guard_name)

guard_span.set_attribute(
SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL"
)
try:
result = _execute_fn(*args, **kwargs)
if isinstance(result, Iterator) and not isinstance(
Expand Down Expand Up @@ -218,6 +224,9 @@ async def trace_async_stream_guard(

add_guard_attributes(guard_span, history, res)
add_user_attributes(guard_span)
guard_span.set_attribute(
SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL"
)
yield res
except StopIteration:
next_exists = False
Expand Down Expand Up @@ -259,7 +268,9 @@ async def trace_async_guard_execution(
guard_span.set_attribute("guardrails.version", GUARDRAILS_VERSION)
guard_span.set_attribute("type", "guardrails/guard")
guard_span.set_attribute("guard.name", guard_name)

guard_span.set_attribute(
SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL"
)
try:
result = await _execute_fn(*args, **kwargs)
if isinstance(result, AsyncIterator):
Expand Down
3 changes: 2 additions & 1 deletion guardrails/telemetry/open_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
recursive_key_operation,
redact,
)
from openinference.semconv.trace import SpanAttributes


def trace_operation(
Expand Down Expand Up @@ -82,7 +83,7 @@ def trace_llm_call(

if current_span is None:
return

current_span.set_attribute(SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL")
ser_function_call = serialize(function_call)
if ser_function_call:
current_span.set_attribute("llm.function_call", ser_function_call)
Expand Down
11 changes: 11 additions & 0 deletions guardrails/telemetry/runner_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Iterator,
Optional,
)
from openinference.semconv.trace import SpanAttributes

from opentelemetry import context, trace
from opentelemetry.trace import StatusCode, Span
Expand All @@ -26,6 +27,7 @@
)
from guardrails.utils.safe_get import safe_get
from guardrails.version import GUARDRAILS_VERSION
from openinference.semconv.trace import SpanAttributes

import sys

Expand Down Expand Up @@ -83,6 +85,9 @@ def trace_step_wrapper(*args, **kwargs) -> Iteration:
name="step", # type: ignore
context=current_otel_context, # type: ignore
) as step_span:
step_span.set_attribute(
SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL"
)
try:
response = fn(*args, **kwargs)
add_step_attributes(step_span, response, *args, **kwargs)
Expand Down Expand Up @@ -111,6 +116,7 @@ def trace_stream_step_generator(
name="step", # type: ignore
context=current_otel_context, # type: ignore
) as step_span:
step_span.set_attribute(SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL")
try:
gen = fn(*args, **kwargs)
next_exists = True
Expand Down Expand Up @@ -157,10 +163,14 @@ async def trace_async_step_wrapper(*args, **kwargs) -> Iteration:
name="step", # type: ignore
context=current_otel_context, # type: ignore
) as step_span:
step_span.set_attribute(
SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL"
)
try:
response = await fn(*args, **kwargs)
add_user_attributes(step_span)
add_step_attributes(step_span, response, *args, **kwargs)

return response
except Exception as e:
step_span.set_status(status=StatusCode.ERROR, description=str(e))
Expand All @@ -186,6 +196,7 @@ async def trace_async_stream_step_generator(
name="step", # type: ignore
context=current_otel_context, # type: ignore
) as step_span:
step_span.set_attribute(SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL")
try:
gen = fn(*args, **kwargs)
next_exists = True
Expand Down
9 changes: 9 additions & 0 deletions guardrails/telemetry/validator_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from opentelemetry import context, trace
from opentelemetry.trace import StatusCode, Tracer, Span
from openinference.semconv.trace import SpanAttributes


from guardrails.settings import settings
Expand Down Expand Up @@ -103,6 +104,10 @@ def trace_validator_wrapper(*args, **kwargs):
name=validator_span_name, # type: ignore
context=current_otel_context, # type: ignore
) as validator_span:
validator_span.set_attribute(
SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL"
)

try:
resp = fn(*args, **kwargs)
add_user_attributes(validator_span)
Expand Down Expand Up @@ -167,6 +172,10 @@ async def trace_validator_wrapper(*args, **kwargs):
name=validator_span_name, # type: ignore
context=current_otel_context, # type: ignore
) as validator_span:
validator_span.set_attribute(
SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL"
) # see here for a list of span kinds: https://github.com/Arize-ai/openinference/blob/main/python/openinference-semantic-conventions/src/openinference/semconv/trace/__init__.py#L271

try:
resp = await fn(*args, **kwargs)
add_user_attributes(validator_span)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "guardrails-ai"
version = "0.6.2"
version = "0.6.3"
description = "Adding guardrails to large language models."
authors = ["Guardrails AI <contact@guardrailsai.com>"]
license = "Apache License 2.0"
Expand Down
Loading