Metadata-Version: 2.4
Name: sovara
Version: 0.6.0
Summary: Python SDK for Sovara agent observability and runtime instrumentation
Project-URL: Documentation, https://docs.sovara-labs.com
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Requires-Dist: dill
Requires-Dist: filelock
Requires-Dist: flatten_dict
Requires-Dist: flatten_json
Requires-Dist: httpx
Requires-Dist: jsonpickle>=4.1.1
Requires-Dist: pydantic
Provides-Extra: examples
Requires-Dist: openai; extra == "examples"
Requires-Dist: anthropic; extra == "examples"
Requires-Dist: openai-agents; extra == "examples"
Requires-Dist: google-genai; extra == "examples"
Requires-Dist: claude-agent-sdk; extra == "examples"

# Sovara Python SDK

The `sovara` package instruments Python agents so Sovara can record LLM calls,
tool calls, logs, and custom trace spans.

Install it in the Python environment where your agent runs:

```bash
python -m pip install sovara
```

Or with `uv`:

```bash
uv add sovara
```

Create a project-bound client and use it to record runs:

```python
from sovara import SovaraClient

sovara_client = SovaraClient(project_name="My Project")

with sovara_client.run("support-agent"):
    run_agent()
```

For a remote/headless agent, pass the exec server and project token directly:

```python
import os

sovara_client = SovaraClient(
    project_name="My Project",
    base_url="https://exec.example.com",
    agent_token=os.environ["SOVARA_AGENT_TOKEN"],
)
```

`http_client=` optionally accepts a caller-owned synchronous `httpx.Client`
for custom proxy, certificate, or transport behavior. Sovara does not close it.

The terminal command is distributed separately as the native Sovara CLI. Install
that CLI for commands such as `sovara record`, `sovara status`, and
`sovara probe`.

Documentation: https://docs.sovara-labs.com

## Explicit conversation turns

`run()` opens a recording session; it does not create an implicit turn. Declare
interactions with scoped `turn()` blocks. Setup, cleanup, and other work outside
those blocks are still captured, with no turn membership.

```python
with sovara_client.run("conversation", run_key="chat-123"):
    setup()
    with sovara_client.turn(turn_input="Hi."):
        respond()
    with sovara_client.turn(turn_input="Another question."):
        respond()
    cleanup()
```

For one recording per interaction, reuse the same project-scoped key and declare
the interaction explicitly:

```python
for message in messages:
    with sovara_client.run("conversation", run_key="chat-123"):
        with sovara_client.turn(turn_input=message):
            respond()
```

Both `run()` and `turn()` support `async with`. A turn block returns its new
`turn_id`, or `None` when the outer run is operating without capture. Exiting the
block restores the previous context; it sends no close request and does not
rotate the recording credential. Empty blocks remain visible as empty turns.
`turn_input` accepts only `str` or `None`, preserves empty strings, Unicode,
spaces, and line breaks exactly, and describes this interaction. Fields written
with `log(run_key=..., run_input=..., run_output=...)` describe the whole run.

Same-run nested turns and using a different client inside the run are rejected.
A subrun's invocation step belongs to the parent's turn, while the child starts
without a turn and may declare its own. Async tasks inherit their originating
turn, and `with_context()` propagates it to threads; another block never reassigns
that work. Await all tasks and consume streams before leaving the outer recording.
Concurrent turn sections are logical groups, not a total timeline.

This Python SDK requires `runner-api` 3. Upgrade app-server and exec-server together;
a server offering only versions 1/2 or no compatibility metadata is rejected before
the agent code starts. An unreachable server retains the existing no-capture
fallback, including scoped turn blocks.

## Explicit logging and eval runs

An eval run represents one evaluation execution, grouping agent runs and their
aggregate results. `create_eval_run()` creates its record; your code executes
the evaluation and logs the results.

```python
eval_run_id = sovara_client.create_eval_run()
with sovara_client.run(eval_run_id=eval_run_id) as run_key:
    sovara_client.log(run_key=run_key, sample_id="case-1", run_input=question, groundtruth=expected_answer)
    answer = agent(question)

sovara_client.log(
    run_key=run_key, eval_run_id=eval_run_id, run_output=answer,
    llm_judge_output="Matches the reference.", llm_judge_is_correct=True,
)
sovara_client.log(eval_run_id=eval_run_id, accuracy=1.0, sample_count=1)
```

Pass `run_key` to log run fields, `eval_run_id` for fields on an eval run, or both
to verify the run belongs to that eval run while logging results. Set ownership with
`run(eval_run_id=...)` before execution; it cannot change afterward. Subruns inherit
the owner. Use a fresh run key per evaluation execution and a stable `sample_id`
for the same case across evaluations. Targets must already exist and are always explicit,
even inside a run context. `groundtruth` is the run's ground-truth annotation, the
same value you edit in the app; log it while the run is active so its analysis
uses it.

Custom fields accept strict booleans, integers (between -2**53 and 2**53), finite
floats, and strings.
Omitted fields remain unchanged; explicit `None` is rejected. The former
`log_input`, `log_output`, and `log_metrics` methods were removed; use `log()`.

Eval run samples show the run's current fields; they are not snapshots.
The application supplies judgments and aggregate metrics, and those metrics are
not recalculated automatically. Evaluation executions appear under Evaluations;
only online executions appear in Runs.

Explicit logging raises on failure. If recording is unavailable, the context
can yield `None`; do not pass that as a logging target. A successful log confirms
database commit, but a timeout leaves the write outcome unknown and is not
automatically retried.
