Skip to content

Commit

Permalink
feat(guardrails): limit support to versions <0.5.2 (#941)
Browse files Browse the repository at this point in the history
  • Loading branch information
harrisonchu authored Aug 22, 2024
1 parent 88ed799 commit 3729e09
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ guardrails hub install hub://guardrails/two_words
```

Set up `GuardrailsInstrumentor` to trace your guardrails application and sends the traces to Phoenix at the endpoint defined below.

```python
from openinference.instrumentation.guardrails import GuardrailsInstrumentor
from opentelemetry import trace as trace_api
Expand All @@ -48,8 +47,8 @@ os.environ["OPENAI_API_KEY"] = "YOUR_KEY_HERE"

endpoint = "http://127.0.0.1:6006/v1/traces"
tracer_provider = trace_sdk.TracerProvider()
trace_api.set_tracer_provider(tracer_provider)
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
trace_api.set_tracer_provider(tracer_provider)

GuardrailsInstrumentor().instrument()
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies = [

[project.optional-dependencies]
instruments = [
"guardrails-ai >= 0.4.5",
"guardrails-ai",
]
test = [
"guardrails-ai == 0.4.5",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import contextvars
import logging
from importlib import import_module
from typing import Any, Collection
from importlib import import_module, metadata
from typing import Any, Collection, Tuple, cast

from opentelemetry import trace as trace_api
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor # type: ignore
Expand All @@ -24,6 +24,11 @@
_LLM_PROVIDERS_MODULE = "guardrails.llm_providers"
_RUNNER_MODULE = "guardrails.run"

GUARDRAILS_VERSION = cast(
Tuple[int, int, int],
tuple(map(int, metadata.version("guardrails-ai").split(".")[:3])),
)


class _Contextvars(ObjectProxy): # type: ignore
def __init__(self, cv: Any) -> None:
Expand All @@ -48,6 +53,10 @@ def instrumentation_dependencies(self) -> Collection[str]:
return _instruments

def _instrument(self, **kwargs: Any) -> None:
if GUARDRAILS_VERSION >= (0, 5, 2):
logger.info("Guardrails version >= 0.5.2 detected, skipping instrumentation")
return

if not (tracer_provider := kwargs.get("tracer_provider")):
tracer_provider = trace_api.get_tracer_provider()
if not (config := kwargs.get("config")):
Expand Down
2 changes: 1 addition & 1 deletion python/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ commands_pre =
langchain-latest: uv pip install -U langchain langchain_core langchain_anthropic langchain_openai langchain_community
langchain_core: uv pip install --reinstall {toxinidir}/instrumentation/openinference-instrumentation-langchain[type-check]
guardrails: uv pip install --reinstall {toxinidir}/instrumentation/openinference-instrumentation-guardrails[test]
guardrails-latest: uv pip install -U guardrails-ai
guardrails-latest: uv pip install -U 'guardrails-ai<0.5.2'
crewai: uv pip install --reinstall {toxinidir}/instrumentation/openinference-instrumentation-crewai[test]
crewai-latest: uv pip install -U crewai
haystack: uv pip install --reinstall {toxinidir}/instrumentation/openinference-instrumentation-haystack[test]
Expand Down

0 comments on commit 3729e09

Please sign in to comment.