Skip to content

Commit

Permalink
fix: defer version detection until run time for guardrails-ai (#1099)
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerHYang authored Nov 1, 2024
1 parent 4dd0bba commit 156b6c1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,13 @@ dependencies = [
"openinference-semantic-conventions",
"wrapt",
"typing-extensions",
"packaging",
]

[project.optional-dependencies]
instruments = [
"guardrails-ai",
]
test = [
"guardrails-ai == 0.4.5",
"opentelemetry-sdk",
"responses",
]

[project.urls]
Homepage = "https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-guardrails"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import contextvars
import logging
from importlib import import_module, metadata
from typing import Any, Collection, Tuple, cast
from typing import Any, Collection

from opentelemetry import trace as trace_api
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor # type: ignore
from packaging.version import Version
from wrapt import ObjectProxy, wrap_function_wrapper

import guardrails as gd
from openinference.instrumentation import OITracer, TraceConfig
from openinference.instrumentation.guardrails._wrap_guard_call import (
_ParseCallableWrapper,
Expand All @@ -18,17 +18,12 @@

logger = logging.getLogger(__name__)

_instruments = ("guardrails-ai >= 0.4.5",)
_instruments = ("guardrails-ai>=0.4.5,<0.5.1",)

_VALIDATION_MODULE = "guardrails.validator_service"
_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 @@ -53,10 +48,13 @@ 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")
version = Version(metadata.version("guardrails-ai"))
if (version.major, version.minor, version.micro) >= (0, 5, 1):
logger.info("Guardrails version >= 0.5.1 detected, skipping instrumentation")
return

import guardrails as gd

if not (tracer_provider := kwargs.get("tracer_provider")):
tracer_provider = trace_api.get_tracer_provider()
if not (config := kwargs.get("config")):
Expand Down Expand Up @@ -109,6 +107,8 @@ def _instrument(self, **kwargs: Any) -> None:
)

def _uninstrument(self, **kwargs: Any) -> None:
import guardrails as gd

# not unwrapping by checking and using the __wrap__ attribute below because the
# original package itself also uses wrapping
if self._original_guardrails_llm_providers_call is not None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
guardrails-ai==0.4.5
opentelemetry-sdk
responses
7 changes: 5 additions & 2 deletions python/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ commands_pre =
langchain: uv pip install --reinstall {toxinidir}/instrumentation/openinference-instrumentation-langchain[test]
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<0.5.2'
guardrails: uv pip uninstall -r test-requirements.txt
guardrails: uv pip install --reinstall-package openinference-instrumentation-guardrails .
guardrails: python -c 'import openinference.instrumentation.guardrails'
guardrails: uv pip install -r test-requirements.txt
guardrails-latest: uv pip install -U 'guardrails-ai<0.5.1'
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 156b6c1

Please sign in to comment.