Skip to main content

aihi-models

[English] | 简体中文

Provider-neutral model contracts and provider adapters for AIHI.

aihi-models is the lowest Python layer in the repository. It normalizes messages, streamed output, tool definitions, usage, provider failures, and wire serialization so the agent runtime does not depend on one vendor SDK.

Why this package exists

The package deliberately owns model-facing primitives only:

  • immutable request/response and content-block contracts;
  • normalized streaming chunks for text and tool input;
  • provider adapters and transport abstractions;
  • typed provider errors and context-length classification;
  • versioned message serialization and token estimation.

It does not own model routing, a gateway, application configuration, prompt policy, sessions, tools, or an agent loop. Those belong to the application/runtime layers.

Supported providers

Adapter Use case Notes
OpenAIProvider OpenAI Chat Completions Configure the endpoint and API key explicitly.
AnthropicProvider Anthropic Messages API Normalizes Anthropic content blocks to the common stream model.
DeepSeekProvider DeepSeek chat models Uses DeepSeek's OpenAI-compatible API by default.
OpenAICompatibleProvider Other OpenAI-compatible endpoints Requires an explicit full base_url for the chat-completions endpoint.
FakeProvider Tests and local contract fixtures Deterministic scripted responses; no network access.

Providers are flat modules under src/aihi/models/providers. Credentials and model selection are supplied by the application; constructors do not silently read environment variables.

Installation

From the repository workspace:

uv sync

For a local editable install:

uv pip install -e packages/aihi/models

The package requires Python 3.11+ and depends on httpx for the default HTTP transport.

Minimal example

import asyncio

from aihi.models import FakeProvider, FakeStep, Message, ModelRequest

provider = FakeProvider([
    FakeStep(text="Hello from a deterministic provider."),
])

request = ModelRequest(
    model="fake-model",
    messages=[Message.text("user", "Say hello.")],
)

async def main() -> None:
    chunks = [chunk async for chunk in provider.stream(request)]
    print(chunks[-1])


asyncio.run(main())

Providers also expose normalized asynchronous streaming. A stream is made of typed chunks such as BlockStart, TextDelta, ToolInputDelta, and MessageEnd, allowing the runtime to render or persist output without vendor-specific branching.

Public API

The package root re-exports the stable building blocks:

  • Contracts: Message, ModelRequest, ModelResponse, ModelToolDefinition, Capabilities, content blocks, and Usage.
  • Providers: OpenAIProvider, AnthropicProvider, DeepSeekProvider, OpenAICompatibleProvider, and FakeProvider.
  • Errors: ProviderError, ProviderHTTPError, ProviderProtocolError, ProviderTimeout, and ProviderContextLengthError.
  • Serialization: encode_message, decode_message, ModelMessageEnvelope, and MESSAGE_SCHEMA_VERSION.
  • Transport: HttpxTransport, JsonTransport, and HttpRequest.

Import from aihi.models rather than reaching into private modules.

Compatibility and errors

Provider adapters map vendor responses into one common response/stream vocabulary and classify failures into stable error types. Callers should handle ProviderTimeout, HTTP failures, protocol failures, and context-length failures separately when deciding whether to retry, compact context, or surface an error.

Message envelopes are versioned. Unknown schema versions raise UnsupportedMessageSchema; applications should persist the versioned envelope rather than relying on a provider's native JSON.

Development

Run package tests and repository-wide static checks from the root:

uv run pytest packages/aihi/models/tests
uv run ruff check packages/aihi/models
uv run mypy

Build a wheel without resolving dependencies from the network:

uv run python -m build --wheel --no-isolation packages/aihi/models

Security notes

  • Pass API keys from the application boundary and keep them out of model messages and persisted events.
  • Treat provider response text and tool arguments as untrusted input.
  • Use the default httpx transport or provide a transport with equivalent timeout and TLS behavior.
  • The fake provider is intended for tests; it is not a fallback for production failures.

Related packages

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aihi_models-0.1.0.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aihi_models-0.1.0-py3-none-any.whl (23.3 kB view details)

Uploaded Python 3

File details

Details for the file aihi_models-0.1.0.tar.gz.

File metadata

  • Download URL: aihi_models-0.1.0.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for aihi_models-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3520a7133bed15084aced76e8817982b66fc3233c060ed82af2dcb7f7faea4c5
MD5 e89bba3ac0fb38f84a3d459d9d470fc0
BLAKE2b-256 7a66ac205228b5aabad0975875ae9875151a4dff80e6498ebcaf10455158697a

See more details on using hashes here.

File details

Details for the file aihi_models-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: aihi_models-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 23.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for aihi_models-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 287f6af203e67237d76878ff239d102761ab9705602f600adae3cd11437e54e8
MD5 9b74a8c1824ed8b5394d6adb1edb760f
BLAKE2b-256 2b54bffdf88d9202f68ae0952d5ecb81a70c1225e562e4fc6916c3ad3e73ab73

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page