Python APIdspy

Core

Instrumentation

Python · Instrumentation

openinference-instrumentation-dspy

OpenInference DSPy Instrumentation

Module
openinference.instrumentation.dspy
Version
0.1.55
Python
>=3.10, <3.15
Instruments
dspy >= 2.6.22

Install

pip install openinference-instrumentation-dspy

Usage

Instrument once, at startup, before the library is used. The DSPyInstrumentor reference below lists the options.

from opentelemetry.sdk.trace import TracerProvider
from openinference.instrumentation import TraceConfig
from openinference.instrumentation.dspy import DSPyInstrumentor

tracer_provider = TracerProvider()
# ...add a span processor that exports to Phoenix or any OTLP backend.

DSPyInstrumentor().instrument(
    tracer_provider=tracer_provider,
    config=TraceConfig(hide_inputs=True),  # optional
)

openinference.instrumentation.dspy

DSPyInstrumentor

classSource ↗
class DSPyInstrumentor(BaseInstrumentor)

OpenInference Instrumentor for DSPy

instrument

method
def instrument(
    *,
    tracer_provider: Optional[TracerProvider] = None,
    config: Optional[TraceConfig] = None,
    skip_dep_check: bool = False,
) -> None

Start tracing dspy: patch it so its calls make OpenInference spans. Calling it again does nothing. Inherited from OpenTelemetry’s BaseInstrumentor.

Parameters

NameDescription
tracer_provider

The tracer provider the spans are made with. Defaults to the global one, opentelemetry.trace.get_tracer_provider().

config

What the spans record: hide inputs, outputs or messages, or limit image sizes. Defaults to TraceConfig(), which reads the OPENINFERENCE_* environment variables. See TraceConfig.

skip_dep_check

Instrument even if the installed version of dspy is outside the supported range.

uninstrument

method
def uninstrument() -> None

Stop tracing: undo the patches instrument() made. Inherited from OpenTelemetry’s BaseInstrumentor.

instrumentation_dependencies

methodSource ↗
def instrumentation_dependencies() -> Collection[str]

CopyableBoundFunctionWrapper

classSource ↗
class CopyableBoundFunctionWrapper(BoundFunctionWrapper)

A bound function wrapper that can be copied and deep-copied. When used to wrap a class method, this allows the entire class to be copied and deep-copied.

For reference, see https://github.com/GrahamDumpleton/wrapt/issues/86#issuecomment-426161271 and https://wrapt.readthedocs.io/en/master/wrappers.html#custom-function-wrappers

CopyableFunctionWrapper

classSource ↗
class CopyableFunctionWrapper(FunctionWrapper)

A function wrapper that can be copied and deep-copied. When used to wrap a class method, this allows the entire class to be copied and deep-copied.

For reference, see https://github.com/GrahamDumpleton/wrapt/issues/86#issuecomment-426161271 and https://wrapt.readthedocs.io/en/master/wrappers.html#custom-function-wrappers

DSPyJSONEncoder

classSource ↗
class DSPyJSONEncoder(json.JSONEncoder)

Provides support for non-JSON-serializable objects in DSPy.

default

methodSource ↗
def default(o: Any) -> Any

SafeJSONEncoder

classSource ↗
class SafeJSONEncoder(json.JSONEncoder)

Safely encodes non-JSON-serializable objects.

default

methodSource ↗
def default(o: Any) -> Any

parse_provider_and_model

functionSource ↗
def parse_provider_and_model(
    model_str: Optional[str],
) -> tuple[Optional[str], Optional[str]]

Parse a model string like ‘openai/gpt-4’, ‘text-completion-openai/gpt-3.5-turbo-instruct/’, etc. Returns (provider, model_name), both lowercased and stripped of trailing slashes.

unwrap_object

functionSource ↗
def unwrap_object(module: Any, name: str) -> Optional[Any]

Undo wrapt-style wrapping by restoring the original method that lives under the wrapped chain.

Parameters

NameTypeDescription
moduleAny

class, instance, or module where the method was wrapped.

namestr

attribute name to unwrap.

Returns

TypeDescription
Optional[Any]

The restored original attribute, or None if not wrapped.