Metadata-Version: 2.5
Name: predikit
Version: 0.7.0
Summary: Wrap any trained sklearn/XGBoost model as an LLM-callable tool: auto-generated JSON schemas, Pydantic I/O, OpenAI & LangChain ready, zero boilerplate.
Project-URL: Homepage, https://github.com/Tejas-TA/predikit
Project-URL: Repository, https://github.com/Tejas-TA/predikit
Project-URL: Documentation, https://github.com/Tejas-TA/predikit#readme
Project-URL: Bug Tracker, https://github.com/Tejas-TA/predikit/issues
Project-URL: Changelog, https://github.com/Tejas-TA/predikit/releases
Author-email: Tejas Tumakuru Ashok <tejasta@gmail.com>
License: MIT
License-File: LICENSE
Keywords: agents,ai-agents,function-calling,langchain,llm,machine-learning,ml-tools,mlops,model-serving,openai,pydantic,scikit-learn,sklearn,tool-use,xgboost
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
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: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: numpy>=1.24
Requires-Dist: pydantic>=2.0
Requires-Dist: scikit-learn>=1.2
Provides-Extra: cli
Requires-Dist: click>=8.0; extra == 'cli'
Requires-Dist: joblib>=1.2; extra == 'cli'
Provides-Extra: dev
Requires-Dist: click>=8.0; extra == 'dev'
Requires-Dist: joblib>=1.2; extra == 'dev'
Requires-Dist: langchain-core>=0.1; extra == 'dev'
Requires-Dist: mcp<2,>=1.28; extra == 'dev'
Requires-Dist: mlflow>=2.0; extra == 'dev'
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: pandas>=1.5; extra == 'dev'
Requires-Dist: pre-commit>=3.0; extra == 'dev'
Requires-Dist: pytest-cov>=4.0; extra == 'dev'
Requires-Dist: pytest>=7.0; extra == 'dev'
Requires-Dist: ruff>=0.4.0; extra == 'dev'
Requires-Dist: xgboost>=1.7; extra == 'dev'
Provides-Extra: langchain
Requires-Dist: langchain-core>=0.1; extra == 'langchain'
Provides-Extra: mcp
Requires-Dist: mcp<2,>=1.28; extra == 'mcp'
Provides-Extra: mlflow
Requires-Dist: mlflow>=2.0; extra == 'mlflow'
Provides-Extra: snowflake
Requires-Dist: snowflake-ml-python>=1.0; extra == 'snowflake'
Provides-Extra: xgboost
Requires-Dist: xgboost>=1.7; extra == 'xgboost'
Description-Content-Type: text/markdown

<p align="center">
  <picture>
    <source srcset="https://raw.githubusercontent.com/Tejas-TA/predikit/main/docs/logo.gif">
    <img src="https://raw.githubusercontent.com/Tejas-TA/predikit/main/docs/logo.png" alt="predikit" width="500"/>
  </picture>
</p>

<p align="center">
  <strong>The bridge between your ML models and LLM agents.</strong><br/>
  Wrap any trained scikit-learn or XGBoost model as an LLM-callable tool —<br/>
  auto-generated JSON schemas, typed I/O, zero boilerplate.
</p>

<p align="center">
  <a href="https://pypi.org/project/predikit/"><img src="https://img.shields.io/pypi/v/predikit.svg" alt="PyPI version"/></a>
  <a href="https://www.python.org/"><img src="https://img.shields.io/badge/python-3.10+-blue.svg" alt="Python 3.10+"/></a>
  <a href="LICENSE"><img src="https://img.shields.io/badge/License-MIT-green.svg" alt="License: MIT"/></a>
  <a href="https://github.com/astral-sh/ruff"><img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json" alt="Ruff"/></a>
</p>

<p align="center">
  <a href="https://github.com/Tejas-TA/predikit/stargazers"><img src="https://img.shields.io/github/stars/Tejas-TA/predikit?style=social" alt="GitHub Stars"/></a>
  <a href="https://github.com/Tejas-TA/predikit/network/members"><img src="https://img.shields.io/github/forks/Tejas-TA/predikit?style=social" alt="GitHub Forks"/></a>
  <a href="https://pepy.tech/project/predikit"><img src="https://static.pepy.tech/personalized-badge/predikit?period=week&units=international_system&left_color=grey&right_color=blue&left_text=weekly+downloads" alt="Weekly Downloads"/></a>
  <a href="https://pepy.tech/project/predikit"><img src="https://static.pepy.tech/personalized-badge/predikit?period=month&units=international_system&left_color=grey&right_color=blue&left_text=monthly+downloads" alt="Monthly Downloads"/></a>
  <a href="https://pepy.tech/project/predikit"><img src="https://static.pepy.tech/personalized-badge/predikit?period=total&units=international_system&left_color=grey&right_color=blue&left_text=total+downloads" alt="Total Downloads"/></a>
</p>

```python
tool = ModelTool(model=clf, name="classify_iris", ...)
tool.to_openai()              # OpenAI function schema, ready to pass to the API
tool.invoke({"sqft": 2200})   # → {"price_usd": 370730}
```

---

## Table of Contents
- [Why predikit?](#why-predikit)
- [Works with](#works-with)
- [Install](#install)
- [Quick start](#quick-start)
- [Core API](#core-api)
- [Field naming rule](#field-naming-rule)
- [Input validation and coercion](#input-validation-and-coercion)
- [Batch and async inference](#batch-and-async-inference)
- [Confidence-aware routing](#confidence-aware-routing)
- [Multi-model ensembles](#multi-model-ensembles)
- [Tool registries and exports](#tool-registries-and-exports)
- [Model registry loaders](#model-registry-loaders)
- [Cookbook](#cookbook)
- [Command line](#command-line)
- [MCP server](#mcp-server)
- [Roadmap](#roadmap)
- [Contributing](#contributing)
- [License](#license)

---

## Why predikit?

Most ML pipelines stop at `.predict()`. Getting that prediction callable by an LLM agent — with validated inputs, correct types, and a schema the model can reason about — requires glue code that's tedious to write and easy to get wrong.

predikit handles that layer for you:

|  | Without predikit | With predikit |
|--|------------------|---------------|
| **Schema** | Hand-write JSON Schema for every model | Auto-generated from your Pydantic `BaseModel` |
| **Type safety** | Manual casting, silent failures | Pydantic v2 validation with clear error messages |
| **LLM integration** | OpenAI / LangChain boilerplate per model | `.to_openai()` / `.to_langchain()` in one line |
| **Ensemble routing** | Custom aggregation logic per project | `ModelEnsemble` with 5 built-in strategies |
| **Confidence handling** | Write your own threshold checks | `confidence_threshold` + `on_low_confidence` |
| **Model registries** | Manual MLflow / Snowflake registry calls | `from_mlflow()` / `from_snowflake()` loaders |
| **Async** | `asyncio.get_event_loop().run_in_executor(...)` | `await tool.ainvoke(inputs)` |

---

## Works with

predikit is designed to plug into the tools your team already uses:

**Models** — scikit-learn · XGBoost

**LLM frameworks** — OpenAI function calling · LangChain · any tool-calling API that accepts JSON Schema

**Model registries** — MLflow Model Registry · Snowflake Model Registry

**Validation** — Pydantic v2

**Async** — `asyncio`-compatible via `ainvoke()`

---

## Install

```bash
pip install predikit

# Optional extras
pip install predikit[xgboost]    # XGBoost support
pip install predikit[langchain]  # LangChain StructuredTool export
pip install predikit[mlflow]     # MLflow Model Registry loader
pip install predikit[snowflake]  # Snowflake Model Registry loader
pip install predikit[cli]        # `predikit inspect` and `predikit serve`
pip install predikit[mcp,cli]    # serve a registry over MCP
```

---

## Quick start

```python
from pydantic import BaseModel, Field
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from predikit import ModelTool

# Train
X, y = load_iris(return_X_y=True)
clf = LogisticRegression(max_iter=200).fit(X, y)

# Define what the LLM will pass in
class IrisInput(BaseModel):
    sepal_length: float = Field(description="Sepal length in cm")
    sepal_width:  float = Field(description="Sepal width in cm")
    petal_length: float = Field(description="Petal length in cm")
    petal_width:  float = Field(description="Petal width in cm")

# Wrap the model
tool = ModelTool(
    model=clf,
    name="classify_iris",
    description="Classify an iris flower: 0=setosa, 1=versicolor, 2=virginica.",
    input_schema=IrisInput,
    output_name="species",
    output_description="Predicted species index",
)

# Get an OpenAI-ready schema
import json
print(json.dumps(tool.to_openai(), indent=2))

# Call it directly
tool.invoke({
    "sepal_length": 5.1, "sepal_width": 3.5,
    "petal_length": 1.4, "petal_width": 0.2,
})
# → {"species": 0}
```

---

## Core API

### `ModelTool`

```python
ModelTool(
    model,               # fitted sklearn-compatible estimator
    name: str,           # tool name the LLM sees
    description: str,    # tool description the LLM sees
    input_schema,        # Pydantic BaseModel describing inputs
    output_name: str,    # key for the prediction in the returned dict
    output_description: str,
    confidence_threshold: float | None = None,
    on_low_confidence: str = "warn",  # "warn" | "raise" | "fallback"
    fallback_tool: ModelTool | None = None,
    verbose: bool = False,
)
```

| Method | Returns | What it does |
|--------|---------|--------------|
| `.invoke(input_dict)` | `dict` | Validates → predicts → returns `{output_name: value}` |
| `.ainvoke(input_dict)` | `dict` | Async version of `.invoke()` |
| `.invoke_many(input_dicts)` | `list[dict]` | Validates and predicts a batch in one model call |
| `.ainvoke_many(input_dicts)` | `list[dict]` | Async version of `.invoke_many()` |
| `.to_openai()` | `dict` | OpenAI function-calling schema |
| `.to_langchain()` | `StructuredTool` | LangChain tool |
| `.to_callable()` | `Callable` | Plain Python function |

### `ToolRegistry`

Group multiple tools for bulk export:

```python
registry = ToolRegistry([price_tool, risk_tool])
registry.to_openai()     # → list[dict], pass directly to OpenAI
registry.to_langchain()  # → list[StructuredTool]
registry.get("name")     # → ModelTool
```

### `ModelEnsemble`

Call multiple models and reconcile their outputs in one step:

```python
ModelEnsemble(
    tools: list[ModelTool],   # models to run in parallel
    name: str,                # ensemble tool name the LLM sees
    description: str,
    strategy: str,            # "collect" | "mean" | "vote" | "weighted_mean" | "weighted_vote"
    weights: list[float],     # optional, for weighted strategies
)
```

| Strategy | Behaviour |
|----------|-----------|
| `"collect"` | Merges all outputs into one dict (tools can have different `output_name`) |
| `"mean"` | Averages numeric outputs (all tools must share `output_name`) |
| `"vote"` | Majority class vote (all tools must share `output_name`) |
| `"weighted_mean"` | Weighted average — provide a `weights` list |
| `"weighted_vote"` | Weighted majority vote — provide a `weights` list |

`ModelEnsemble` exposes the same `.invoke()`, `.ainvoke()`, `.invoke_many()`, `.ainvoke_many()`, `.to_openai()`, and `.to_langchain()` interface as `ModelTool`.

## Input validation and coercion

Inputs are validated by the Pydantic model before an estimator runs. This produces useful errors
for missing fields and incompatible values, while common LLM scalar strings are handled before
validation: numeric strings work for `int` and `float`; boolean fields accept `true/false`,
`yes/no`, `1/0`, and `on/off`.

For DataFrame-trained models, schema field names must match training columns exactly; predikit
uses the estimator's feature names to order values. For array-trained models, it verifies the
schema width and uses schema definition order.

## Batch and async inference

For application-side workloads, pass several input dictionaries at once. predikit validates every
record before invoking the model, makes one `predict()` call, and returns results in the same order:

```python
predictions = tool.invoke_many([
    {"sepal_length": 5.1, "sepal_width": 3.5, "petal_length": 1.4, "petal_width": 0.2},
    {"sepal_length": 6.0, "sepal_width": 2.2, "petal_length": 4.0, "petal_width": 1.0},
])
# → [{"species": 0}, {"species": 1}]
```

Use `await tool.ainvoke_many(inputs)` in async applications. Ensembles support the same batch
methods and preserve their configured reconciliation strategy per input.

---

## Field naming rule

**Your Pydantic schema field names must exactly match the column names the model was trained on.**

predikit maps inputs to features by name, not position. If you trained on a DataFrame with columns `["sqft", "bedrooms"]`, your schema fields must be `sqft` and `bedrooms` — not `sq_ft`, not `Sqft`.

```python
# ✓ Columns match: sqft, bedrooms, bathrooms
class GoodInput(BaseModel):
    sqft:      float
    bedrooms:  float
    bathrooms: float

# ✗ Name mismatch — raises ValueError at runtime
class BadInput(BaseModel):
    square_footage: float  # model expects "sqft"
    beds:           float  # model expects "bedrooms"
    baths:          float  # model expects "bathrooms"
```

When there's a mismatch, predikit tells you exactly which names are wrong:

```
ValueError: Input schema is missing model features: ['sqft', 'bedrooms'].
Schema has: ['square_footage', 'beds', 'bathrooms'], model expects: ['sqft', 'bedrooms', 'bathrooms']
```

> **Tip:** If you trained with a numpy array (no DataFrame), predikit has no feature names to check — it uses your schema's field definition order instead.

---

## Tool registries and exports

Use a `ToolRegistry` when an agent needs several models. It rejects duplicate names and exports
the same complete set of tools and ensembles to OpenAI, LangChain, or MCP. `to_openai()` returns
OpenAI-compatible function-tool dictionaries built from Pydantic JSON Schema; `to_langchain()`
returns `StructuredTool` objects with sync and async execution; `to_callable()` returns a normal
keyword-only Python function.

## Multi-model ensembles

`ModelEnsemble` applies one input to several tools, then reconciles the results. `collect` merges
distinct outputs; `mean` and `weighted_mean` average numeric outputs; `vote` and `weighted_vote`
select a class. Aggregate strategies require a shared `output_name`; collect requires unique ones.
Async ensembles run member tools concurrently, and batch methods reconcile every input row.

## Confidence-aware routing

For classifiers exposing `predict_proba`, `confidence_threshold` adds per-prediction handling:
`warn` returns `_confidence` and `_low_confidence`, `raise` emits `LowConfidenceError`, and
`fallback` invokes a second `ModelTool`. Regressors and classifiers without probabilities remain
usable and warn at construction when this setting cannot apply.

## Model registry loaders

`from_mlflow()` loads an MLflow pyfunc model from a URI, while `from_snowflake()` loads a model
version with an active Snowpark session. Both return ordinary `ModelTool` objects, so exports,
ensembles, batches, and confidence routing work unchanged.

## Cookbook

### XGBoost regression

```python
from xgboost import XGBRegressor
from predikit import ModelTool

reg = XGBRegressor().fit(X_train, y_train)

class HouseInput(BaseModel):
    sqft:       float
    bedrooms:   float
    year_built: float

tool = ModelTool(
    model=reg,
    name="price_estimate",
    description="Predict home price in USD.",
    input_schema=HouseInput,
    output_name="price_usd",
    output_description="Predicted sale price in USD",
)
```

### Multiple tools in one registry

```python
registry = ToolRegistry([price_tool, risk_tool, demand_tool])

# OpenAI
response = client.chat.completions.create(
    model="gpt-4o",
    tools=registry.to_openai(),
    ...
)

# LangChain
agent = initialize_agent(tools=registry.to_langchain(), ...)
```

## MCP server

Expose the same registry to MCP-compatible clients with the optional MCP extra:

```bash
pip install predikit[mcp,cli]
predikit serve my_tools:registry
```

`my_tools:registry` must point to a `ToolRegistry`, or to a zero-argument function
that returns one. The default transport is `stdio`; use `--transport streamable-http`
when serving over HTTP, optionally with `--host` and `--port`. MCP tools advertise
their output schemas and use the same Pydantic input validation and model execution
as direct `invoke()` calls. For an in-process server instead of the CLI, install
`predikit[mcp]` and call `create_mcp_server(registry)`.

## Command line

Install `predikit[cli]` for model inspection and MCP serving:

```bash
# Print model task, feature metadata, and an OpenAI schema for a joblib model.
predikit inspect path/to/model.joblib --name price_estimate --description "Estimate home price"

# Serve a module-level registry, or a zero-argument registry factory, over MCP stdio.
predikit serve my_tools:registry

# Serve Streamable HTTP on an explicit interface and port.
predikit serve my_tools:make_registry --transport streamable-http --host 0.0.0.0 --port 9123
```

`inspect` can generate a schema for models trained with named DataFrame columns. `serve` defaults
to stdio and requires `predikit[mcp,cli]`.

### Bool inputs from an LLM

LLMs sometimes return `"yes"`, `"true"`, or `"1"` for boolean fields. predikit coerces these automatically before Pydantic validation:

```python
class Input(BaseModel):
    has_pool: bool

tool.invoke({"has_pool": "yes"})   # → coerced to True
tool.invoke({"has_pool": "false"}) # → coerced to False
tool.invoke({"has_pool": "maybe"}) # → raises ValueError with clear message
```

Supported strings: `true/false`, `yes/no`, `1/0`, `on/off`.

### Confidence-aware routing

Route uncertain predictions to a fallback tool, or raise an error the agent can catch:

```python
from predikit import ModelTool, LowConfidenceError

tool = ModelTool(
    model=clf,
    name="churn_risk",
    description="Predict member churn risk.",
    input_schema=MemberInput,
    output_name="churn_probability",
    output_description="Probability of churn (0–1)",
    confidence_threshold=0.80,       # classifiers with predict_proba only
    on_low_confidence="warn",        # "warn" | "raise" | "fallback"
    fallback_tool=rule_based_tool,   # used when mode="fallback"
)

result = tool.invoke(inputs)
if result.get("_low_confidence"):
    print(f"Uncertain ({result['_confidence']:.2f}) — consider routing to a human")
```

| mode | behaviour |
|------|-----------|
| `"warn"` | returns prediction + `_confidence` + `_low_confidence: True` |
| `"raise"` | raises `LowConfidenceError` |
| `"fallback"` | invokes `fallback_tool` and returns its result |

Only applies to classifiers that implement `predict_proba`. Regressors are unaffected.

### Multi-model ensemble

```python
from predikit import ModelEnsemble, ToolRegistry

ensemble = ModelEnsemble(
    tools=[price_tool_a, price_tool_b],
    name="averaged_price",
    description="Ensemble price: mean of two XGBoost models.",
    strategy="mean",              # "collect" | "mean" | "vote"
)

result  = ensemble.invoke(inputs)  # → {"price_usd": 370112}
schema  = ensemble.to_openai()     # works exactly like ModelTool
```

Register ensembles alongside individual tools:

```python
registry = ToolRegistry(tools=[price_tool], ensembles=[ensemble])
registry.to_openai()  # includes both tools and ensembles
```

### MLflow Model Registry loader

Load a registered MLflow model directly — no manual `.load_model()` call:

```python
from predikit.loaders import from_mlflow

tool = from_mlflow(
    model_uri="models:/churn-classifier/Production",
    name="churn_risk",
    description="Predict member churn probability.",
    input_schema=MemberInput,
    output_name="churn_probability",
    output_description="Churn probability 0–1",
)

tool.invoke({"tenure_months": 24, "trips_last_year": 2, "avg_spend": 500})
# → {"churn_probability": 0.73}
```

The loader auto-detects `classes_` and `feature_names_in_` from the underlying sklearn model, so confidence routing and ensemble work unchanged. Requires `pip install predikit[mlflow]`.

### Snowflake Model Registry loader

Load a model registered in the Snowflake Model Registry via the Snowpark ML Python library:

```python
from predikit.loaders import from_snowflake

tool = from_snowflake(
    session=snowpark_session,
    model_name="VACATION_CHURN",
    model_version="V3",
    name="churn_risk",
    description="Churn classifier.",
    input_schema=MemberInput,
    output_name="churn_probability",
    output_description="Churn probability 0–1",
    output_method="predict",   # method to call on the Snowflake model object
)
```

Pass `output_method="predict_proba"` or any other method your Snowflake model exposes. The returned `ModelTool` is identical to one built directly — all exporters, confidence routing, and ensemble strategies work as-is. Requires `pip install predikit[snowflake]`.

## Examples

See [`examples/03_orlando_real_estate.py`](examples/03_orlando_real_estate.py) for a full walkthrough: synthetic dataset → XGBoost training → `ModelTool` → registry → OpenAI schema → prediction.

---

## Roadmap

### Shipped

- [x] OpenAI function-calling schema export
- [x] LangChain `StructuredTool` export
- [x] Pydantic v2 typed I/O validation
- [x] Multi-model `ModelEnsemble` — collect / mean / vote / weighted variants
- [x] Confidence routing — `warn` / `raise` / `fallback`
- [x] Async `ainvoke()` via thread pool executor
- [x] MLflow Model Registry loader
- [x] Snowflake Model Registry loader
- [x] `predikit inspect` CLI
- [x] Batch inference (`invoke_many` / `ainvoke_many`)

### Planned

- [ ] HuggingFace / PyTorch / TensorFlow model support
- [ ] Streaming inference support
- [ ] OpenAI Assistants API integration
- [x] MCP server mode (stdio and Streamable HTTP)

---

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup, code style, and PR guidelines. The [CHANGELOG](CHANGELOG.md) tracks notable changes per release.

Issues and PRs are welcome — if you're wrapping a model type or registry that predikit doesn't support yet, open a discussion.

## License

MIT © Tejas Tumakuru Ashok
