Skip to content
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

fix: defer version detection until run time for guardrails-ai #1099

Merged
merged 5 commits into from
Nov 1, 2024
Merged
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
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
Loading