# openinference-instrumentation-dspy

**Python · Instrumentation**

OpenInference DSPy Instrumentation

- **Module**: `openinference.instrumentation.dspy`
- **Version**: 0.1.56
- **Python**: >=3.10, <3.15
- **Instruments**: `dspy >= 2.6.22`

[PyPI](https://pypi.org/project/openinference-instrumentation-dspy/) · [README](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-dspy/README.md) · [Source](https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-dspy)

## Install

```sh
pip install openinference-instrumentation-dspy
```

## Usage

Instrument once, at startup, before the library is used. The [`DSPyInstrumentor`](https://openinf.dev/python/openinference-instrumentation-dspy.html.md#openinference.instrumentation.dspy.DSPyInstrumentor) reference below lists the options.

```python
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`

_class_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-dspy/src/openinference/instrumentation/dspy/__init__.py#L76-L251)

```python
class DSPyInstrumentor(BaseInstrumentor)
```

OpenInference Instrumentor for DSPy

#### `instrument`

_method_

```python
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`](https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/base/instrumentor.html).

**Parameters**

| Name | Description |
| - | - |
| `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`](https://openinf.dev/python/openinference-instrumentation.html.md#openinference.instrumentation.TraceConfig). |
| `skip_dep_check` | Instrument even if the installed version of `dspy` is outside the supported range. |

#### `uninstrument`

_method_

```python
def uninstrument() -> None
```

Stop tracing: undo the patches `instrument()` made. Inherited from OpenTelemetry’s [`BaseInstrumentor`](https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/base/instrumentor.html).

#### `instrumentation_dependencies`

_method_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-dspy/src/openinference/instrumentation/dspy/__init__.py#L98-L99)

```python
def instrumentation_dependencies() -> Collection[str]
```

### `CopyableBoundFunctionWrapper`

_class_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-dspy/src/openinference/instrumentation/dspy/__init__.py#L254-L274)

```python
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`

_class_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-dspy/src/openinference/instrumentation/dspy/__init__.py#L277-L294)

```python
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`

_class_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-dspy/src/openinference/instrumentation/dspy/__init__.py#L922-L939)

```python
class DSPyJSONEncoder(json.JSONEncoder)
```

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

#### `default`

_method_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-dspy/src/openinference/instrumentation/dspy/__init__.py#L927-L939)

```python
def default(o: Any) -> Any
```

### `SafeJSONEncoder`

_class_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-dspy/src/openinference/instrumentation/dspy/__init__.py#L942-L953)

```python
class SafeJSONEncoder(json.JSONEncoder)
```

Safely encodes non-JSON-serializable objects.

#### `default`

_method_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-dspy/src/openinference/instrumentation/dspy/__init__.py#L947-L953)

```python
def default(o: Any) -> Any
```

### `parse_provider_and_model`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-dspy/src/openinference/instrumentation/dspy/__init__.py#L1047-L1063)

```python
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`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-dspy/src/openinference/instrumentation/dspy/__init__.py#L1317-L1345)

```python
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**

| Name | Type | Description |
| - | - | - |
| `module` | `Any` | class, instance, or module where the method was wrapped. |
| `name` | `str` | attribute name to unwrap. |

**Returns**

| Type | Description |
| - | - |
| `Optional[Any]` | The restored original attribute, or None if not wrapped. |
