Metadata-Version: 2.4
Name: mlflow-neomundi
Version: 0.1.1
Summary: Measure the model calls MLflow already traces with the NeoMundi measurement API
Author-email: Sebastien FAVRE <sebastien@neomundi.org>
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/neomundi-io/neomundi-integrations/tree/main/integrations/mlflow
Project-URL: Repository, https://github.com/neomundi-io/neomundi-integrations
Project-URL: Documentation, https://github.com/neomundi-io/neomundi-integrations/tree/main/integrations/mlflow#readme
Project-URL: Changelog, https://github.com/neomundi-io/neomundi-integrations/blob/main/integrations/mlflow/CHANGELOG.md
Project-URL: Issues, https://github.com/neomundi-io/neomundi-integrations/issues
Keywords: mlflow,tracing,genai,neomundi,ai-governance,llm-measurement,observability
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: System :: Monitoring
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown
Requires-Dist: mlflow-tracing<3.19,>=3.18.0
Requires-Dist: neomundi<3.0,>=2.1.0
Provides-Extra: dev
Requires-Dist: pytest>=8.0; extra == "dev"
Requires-Dist: openai>=1.26.0; extra == "dev"
Requires-Dist: mlflow-skinny<3.19,>=3.18.0; extra == "dev"
Requires-Dist: sqlalchemy<3,>=1.4.0; extra == "dev"
Requires-Dist: alembic<2,>=1.10.1; extra == "dev"
Requires-Dist: langchain-core<2,>=1.0; extra == "dev"
Requires-Dist: openai-agents<0.27,>=0.26.0; extra == "dev"
Provides-Extra: examples
Requires-Dist: openai>=1.26.0; extra == "examples"
Requires-Dist: mlflow-skinny<3.19,>=3.18.0; extra == "examples"
Requires-Dist: sqlalchemy<3,>=1.4.0; extra == "examples"
Requires-Dist: alembic<2,>=1.10.1; extra == "examples"
Provides-Extra: notebook
Requires-Dist: nbformat>=5.9; extra == "notebook"
Requires-Dist: nbclient>=0.9; extra == "notebook"
Requires-Dist: ipykernel>=6.29; extra == "notebook"

<div align="center">

[![English](https://img.shields.io/badge/EN-English-2563eb?style=flat-square)](README.md) · [![Français](https://img.shields.io/badge/FR-Fran%C3%A7ais-6b7280?style=flat-square)](README_FR.md)

# MLflow × NeoMundi

> 🟢 **New here? Start with [Getting Started — From Integration to Verifiable Evidence](https://github.com/neomundi-io/neomundi-integrations/blob/main/GETTING_STARTED.md).** It is the **single source of truth** for the whole path: exact versions, executable commands, measured outputs, the common errors — and 🔴 **which steps are still blocked, and why**.

**Measure the model calls MLflow already traces, and find each measurement on the MLflow trace. One function next to your own span processors, the latency MLflow recorded, and only token counts the provider published.**

**[Quickstart](#quickstart)** · **[What it refuses to do](#refusals)** · **[Notebook](notebooks/quickstart.ipynb)** · **[Limitations](LIMITATIONS.md)** · **[Cost](#what-it-costs)**

</div>

---

> **Evidence level: E2.** Four real model calls traced by the real
> `mlflow.openai.autolog()` against a local Ollama's OpenAI-compatible
> `/v1` were measured on the service verification account on 2026-11-26
> and re-read from the API as persisted: 4/4, served
> `measurement_version` 3.5.0, 0.12 CHF. Two more phases were refused on
> real spans and billed nothing: a stream without usage, and a span
> carrying a count of unknown origin. Proof: [`evidence/e2_qualification_20261126T165539Z.json`](evidence/e2_qualification_20261126T165539Z.json). See
> [LIMITATIONS.md §8](LIMITATIONS.md#8-evidence-level-e2-one-writer-one-provider).

## What this is

`NeoMundiMlflowProcessor` registers **one function** with MLflow's public
hook, `mlflow.tracing.configure(span_processors=[...])`. It is **appended**
to the processors you already have, and MLflow calls it for every span it
ends. For each model call written by MLflow's `openai` autolog it takes
three things:

- the token count, as the provider's own `total_tokens`, copied by MLflow
  into `mlflow.chat.tokenUsage`;
- the latency, as the span's `end_time_ns - start_time_ns` that MLflow
  recorded;
- the served model.

It then submits to NeoMundi's `POST /v1/govern` through the `neomundi` SDK,
on a worker thread, so your call never waits for it. Afterwards it writes
the NeoMundi `request_id` and `measurement_version` **back on the MLflow
trace** as tags. You can open the trace in the MLflow UI and find the
observation that measured it.

Every other span gets an outcome too, with a named status. Agent, chain,
workflow and task spans are aggregates. Tools, retrievers and embeddings
are not model calls. A count written by any other instrumentation is
unverified.

## What it is not

- **Not a measurement of every MLflow autolog.** MLflow stores token counts from many writers under one attribute, and some of them are not the provider's. We measured three cases where the provider published **nothing**: the Agents SDK tracer writes `0/0/0`, LiteLLM's MLflow logger writes its own estimate (`14/7/21`), and LangChain copies whatever the chat model computed. Only the `openai` autolog was shown to copy the provider's usage verbatim and to leave it absent when there is none ([LIMITATIONS §1](LIMITATIONS.md#1-only-the-openai-autolog-is-measured--by-design)).
- **Not an OpenTelemetry processor.** A processor added to MLflow's tracer provider is silently dropped on the next `mlflow.set_experiment()`. This uses MLflow's own public hook instead ([§2](LIMITATIONS.md#2-staying-attached-what-removes-the-hook)).
- **Not a reader of the trace-level aggregate.** MLflow's trace total sums calls that are measured one by one.
- **Not a tracer, evaluator or truth checker.** Every submission is a billed measurement. NeoMundi verifies integrity and authenticity, not whether an answer was true.

## Install

```bash
pip install mlflow-neomundi
```

Python ≥ 3.11. Depends on `mlflow-tracing>=3.18.0,<3.19`, the light
package a full `mlflow` already includes, and on `neomundi>=2.1.0,<3.0`.
MLflow needs a store to trace at all: with `mlflow-tracing` alone and no
tracking URI, it records no span ([§5](LIMITATIONS.md#5-mlflow-must-be-able-to-trace)).


🔴 **`pip install` does not ship `examples/`, `notebooks/` or `evidence/`.** Measured on 2026-12-01 against the eight published wheels: each ships its package and its metadata, and **none** carries those directories. They live in the repository — [`integrations/mlflow/`](https://github.com/neomundi-io/neomundi-integrations/tree/main/integrations/mlflow) — so every path this page states relative to them is a repository path, not a file your install contains.
## Quickstart

⚠ **A wiring sketch, not a runnable program, and it needs two things this page did not state.** Measured on a bare `pip install mlflow-neomundi`: `mlflow.openai.autolog()` raises `ModuleNotFoundError: No module named 'openai'` — **`openai` is not a dependency of this package** — and the tracking URI it sets points at an **MLflow server you must be running**. It also assumes your own `client`. **The block that runs as published is just below**, and the fuller example is [`examples/01_offline_autolog.py`](https://github.com/neomundi-io/neomundi-integrations/blob/main/integrations/mlflow/examples/01_offline_autolog.py), 🔴 **in the repository, not in the wheel**, and it needs the `examples` extra.

```python
import os

import mlflow
from mlflow_neomundi import NeoMundiMlflowProcessor

mlflow.set_tracking_uri("http://localhost:5000")  # first: a new URI resets MLflow's hooks
mlflow.openai.autolog()

processor = NeoMundiMlflowProcessor(
    api_key=os.environ["NEOMUNDI_API_KEY"],
    base_url="https://api.neomundi.io",
).install()

reply = client.chat.completions.create(model="gpt-4o-mini", messages=[...])
processor.flush()  # submissions run off your call's path

for o in processor.outcomes:
    print(o.span_type, o.status, o.token_count, o.latency_ms, o.request_id)
print(processor.installed)  # is MLflow still calling it?
```

### Runnable as published — no key, no provider, no network

🔴 **This one needs one more install, and the page used to be silent about it.** Measured on a bare `pip install mlflow-neomundi`: `sqlalchemy` is absent, so a sqlite tracking store cannot be opened, and `mlflow-tracing` alone records **no span** at all ([§5](LIMITATIONS.md#5-mlflow-must-be-able-to-trace)). `openai` is not a dependency either. The integration's own extra installs exactly what this block needs:

```sh
pip install "mlflow-neomundi[examples]"
```

🟢 **Then it runs with no key and no provider** — verified in a fresh virtual environment with that command and nothing else.

```python
import os
import tempfile

# MLflow reads these when it is imported, so they come first.
os.environ["MLFLOW_DISABLE_TELEMETRY"] = "true"
os.environ["DO_NOT_TRACK"] = "true"
_store = tempfile.mkdtemp().replace("\\", "/")
os.environ["MLFLOW_TRACKING_URI"] = f"sqlite:///{_store}/mlflow.db"

import httpx  # noqa: E402
import mlflow  # noqa: E402
from openai import OpenAI  # noqa: E402

from mlflow_neomundi import NeoMundiMlflowProcessor  # noqa: E402


def stub_provider() -> OpenAI:
    """No provider is called: the transport answers with a canned reply."""

    def handler(request: httpx.Request) -> httpx.Response:
        return httpx.Response(
            200,
            json={
                "id": "chatcmpl-quickstart",
                "object": "chat.completion",
                "created": 1764300000,
                "model": "stub-model",
                "choices": [
                    {
                        "index": 0,
                        "message": {"role": "assistant", "content": "Paris."},
                        "finish_reason": "stop",
                    }
                ],
                # The counts a PROVIDER would have declared.
                "usage": {"prompt_tokens": 11, "completion_tokens": 3, "total_tokens": 14},
            },
        )

    return OpenAI(
        api_key="not-a-real-key",
        base_url="https://provider.example.invalid/v1",
        max_retries=0,
        http_client=httpx.Client(transport=httpx.MockTransport(handler)),
    )


mlflow.set_experiment("mlflow-neomundi-quickstart")
mlflow.openai.autolog()
client = stub_provider()

# Install AFTER the tracking URI is set: a new URI resets MLflow's hooks.
processor = NeoMundiMlflowProcessor(submit=False).install()  # no key: nothing is sent

reply = client.chat.completions.create(
    model="stub-model", messages=[{"role": "user", "content": "Capital of France?"}]
)
processor.flush()  # submissions run on a worker thread, off your call's path

print("answer :", reply.choices[0].message.content)
for outcome in processor.outcomes:
    line = f"{outcome.span_type:<11} {outcome.status}"
    if outcome.token_count is not None:
        line += f" tokens={outcome.token_count} writer={outcome.writer}"
    print(line)
```

It prints exactly this:

```
answer : Paris.
CHAT_MODEL  not_submitted_local_only tokens=14 writer=openai
```

### Where a real key comes from

**Ask for one at <https://neomundi.io/contact-us/>** — verified reachable, and
the only route this page states. ⚠ It is **not** a claim that no other route
exists; it is the one we can point you to today.

🟢 **You do not need one to evaluate this package.** The offline test suite
needs no credential, and the evidence committed in `evidence/` can be checked
without an account.


Free first: `NeoMundiMlflowProcessor(submit=False).install()` needs no
key, opens no socket, and runs every check. It shows, on your own traffic,
which calls would be measured and which would be refused, and why.

## Refusals

Each status is a constant exported by the package. Every refusal submits
nothing.

| Status | When |
|---|---|
| `not_submitted_unverified_token_count` | the span was written by something other than MLflow's `openai` autolog: the Agents SDK tracer, LangChain, LiteLLM's logger, a hand-written span |
| `not_submitted_no_token_count` | the provider published no usage, so the attribute is absent and stays absent |
| `not_submitted_invalid_token_count` | usage without a positive integer `total_tokens` (partial, null, zero); never rebuilt |
| `not_submitted_aggregate_span` | `AGENT`, `CHAIN`, `WORKFLOW`, `TASK`: they contain calls already measured |
| `not_submitted_not_a_model_call` | `TOOL`, `RETRIEVER`, `RERANKER`, `EMBEDDING`, `GUARDRAIL`, `PARSER`, `MEMORY`, `EVALUATOR` |
| `not_submitted_unsupported_span` | `UNKNOWN`, or a span type this release does not know |
| `not_submitted_application_error` | the model call failed (span status `ERROR`) |
| `not_submitted_no_latency` | no usable start and end time |
| `skipped_sampling` | excluded by `sample_rate` |
| `not_submitted_local_only` | `submit=False` |
| `not_submitted_transport_error` | the NeoMundi API was unreachable or refused; your call is untouched |
| `not_submitted_queue_full`, `not_submitted_processor_closed`, `not_submitted_internal_error` | the processor protected your application |

Outcomes never carry the prompt, the response or a key. They carry only
counts, names and identifiers. [`examples/02_refusals.py`](examples/02_refusals.py)
shows each refusal on a real MLflow span.

## What it costs

🔴 **Every submitted observation is a billed measurement** (0.030 CHF on
TIER_03). One observation per measured model call. Aggregate spans are
never billed, so an agent run under `mlflow.openai.autolog()` is not
counted twice. `sample_rate` (default `1.0`) is deterministic in the span
id. **Kill switch:** `processor.sample_rate = 0.0`, immediate. Installing a
second processor retires the first, so re-running a notebook cell does not
bill twice.

## Honest status

| | |
|---|---|
| Offline test suite | 75 tests, green on Python 3.12 / Linux locally; CI runs 3.11–3.13 on Linux, macOS, Windows |
| Real execution | **E2**: 4 observations submitted to `https://api.neomundi.io`, 4/4 re-read as persisted (2026-11-26, Python 3.12, `neomundi` 2.1.0). See [`evidence/`](evidence/) |
| Maturity | beta |

Read [LIMITATIONS.md](LIMITATIONS.md) before drawing a conclusion from any score.

## License

Apache-2.0. See [LICENSE](../../LICENSE).
