Metadata-Version: 2.5
Name: openinference-instrumentation-cohere
Version: 0.1.5
Summary: OpenInference Cohere Instrumentation
Project-URL: Homepage, https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-cohere
Author-email: OpenInference Authors <oss@arize.com>
License-Expression: Apache-2.0
License-File: LICENSE
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Requires-Python: <3.15,>=3.10
Requires-Dist: openinference-instrumentation>=0.1.51
Requires-Dist: openinference-semantic-conventions>=0.1.33
Requires-Dist: opentelemetry-api
Requires-Dist: opentelemetry-instrumentation
Requires-Dist: opentelemetry-semantic-conventions
Requires-Dist: wrapt
Provides-Extra: instruments
Requires-Dist: cohere>=5.13.0; extra == 'instruments'
Description-Content-Type: text/markdown

# OpenInference Cohere Instrumentation

[![pypi](https://badge.fury.io/py/openinference-instrumentation-cohere.svg)](https://pypi.org/project/openinference-instrumentation-cohere/)

Python auto-instrumentation library for the [Cohere](https://github.com/cohere-ai/cohere-python) Python client.

Chat, embedding, and rerank calls made with the Cohere v2 client (`ClientV2` and
`AsyncClientV2`) are traced and exported as OpenInference spans. Chat spans capture messages,
invocation parameters, tool calls, and token counts. Embedding spans capture the model name,
input text, invocation parameters, vectors, and token counts. Rerank spans capture the query,
model, input documents, ranked output documents, and relevance scores.

## Coverage

`ClientV2.chat`, `AsyncClientV2.chat`, `ClientV2.chat_stream`,
`AsyncClientV2.chat_stream`, `ClientV2.embed`, `AsyncClientV2.embed`, `ClientV2.rerank`, and
`AsyncClientV2.rerank` are instrumented.
Streamed calls finish their span when the returned iterator is exhausted, with the accumulated
output message, tool calls, and token counts.

The following are **not** traced, and calls to them produce no spans:

- The v1 client (`cohere.Client`)
- Classify endpoints

## Installation

```shell
pip install openinference-instrumentation-cohere
```

PyPI package: [`openinference-instrumentation-cohere`](https://pypi.org/project/openinference-instrumentation-cohere/)

## Quickstart

Install packages needed for this demonstration.

```shell
pip install openinference-instrumentation-cohere cohere arize-phoenix opentelemetry-sdk opentelemetry-exporter-otlp
```

Start [Phoenix](https://github.com/Arize-ai/phoenix) in the background as a collector. By default, it listens on `http://localhost:6006`.

```shell
phoenix serve
```

Set up `CohereInstrumentor` to trace your application and send the traces to Phoenix.

```python
from openinference.instrumentation.cohere import CohereInstrumentor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

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

CohereInstrumentor().instrument(tracer_provider=tracer_provider)
```

Run a chat request. Set the `CO_API_KEY` environment variable with your Cohere API key.

```python
import cohere

co = cohere.ClientV2()
response = co.chat(
    model="command-a-03-2025",
    messages=[{"role": "user", "content": "Why is the sky blue?"}],
)
print(response.message.content[0].text)
```

## More Info

- [OpenInference](https://github.com/Arize-ai/openinference)
- [Cohere Python client](https://github.com/cohere-ai/cohere-python)
