Skip to main content

ollama-instructor

ollama-instructor is a lightweight Python library that provides a convenient wrapper around the Ollama Client, extending it with validation features for obtaining valid JSON responses from Large Language Models (LLMs). Utilizing Pydantic, ollama-instructor ensures that responses from LLMs adhere to defined schemas.

Downloads

Note: This library depends on having Ollama installed and running. For more information, please refer to the official website of Ollama.

Breaking Changes in Version 1.0.0

Version 1.0.0 introduces significant changes from version 0.5.2:

  • Complete refactoring to directly inherit from Ollama's official Client classes
  • Simplified API that aligns more closely with Ollama's native interface
  • Improved logging system using Python's built-in logging module
  • Streamlined validation process using Pydantic
  • Removal of partial validation features to focus on core functionality
  • New method names: chat_completion and chat_stream (previously chat_completion_with_stream)

Features

  • Direct Integration: Inherits directly from Ollama's official client for seamless integration
  • Schema Validation: Uses Pydantic BaseModel to ensure valid JSON responses
  • Retry Mechanism: Automatically retries failed validations with configurable attempts
  • Logging: Comprehensive logging system with configurable levels
  • Async Support: Full async/await support through OllamaInstructorAsync

Installation

pip install ollama-instructor

Quick Start

For streaming examples click here

Synchronous Usage:

from pydantic import BaseModel
from ollama_instructor import OllamaInstructor

class FriendInfo(BaseModel):
    name: str
    age: int
    is_available: bool

class FriendList(BaseModel):
    friends: list[FriendInfo]

# Create client with logging enabled
client = OllamaInstructor(enable_logging=True, log_level='DEBUG')

# Chat completion
response = client.chat_completion(
    format=FriendList,
    model='llama2:latest',
    messages=[
        {
            'role': 'user',
            'content': 'I have two friends: John (25, available) and Mary (30, busy)'
        }
    ]
)

Asynchronous Usage:

import asyncio
from pydantic import BaseModel
from ollama_instructor import OllamaInstructorAsync

class FriendInfo(BaseModel):
    name: str
    age: int
    is_available: bool

async def main():
    client = OllamaInstructorAsync(enable_logging=True)

    response = await client.chat_completion(
        format=FriendInfo,
        model='llama2:latest',
        messages=[
            {
                'role': 'user',
                'content': 'John is 25 years old and available to hang out'
            }
        ]
    )

if __name__ == "__main__":
    asyncio.run(main())

Logging

The library includes comprehensive logging capabilities. You can enable and configure logging when initializing the client:

client = OllamaInstructor(
    enable_logging=True,
    log_level="DEBUG",  # Options: DEBUG, INFO, WARNING, ERROR, CRITICAL
    log_format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)

Support and Community

If you need help or want to discuss ollama-instructor, feel free to:

Contributions and feedback are always welcome! 😊

License

ollama-instructor is released under the MIT License. See the LICENSE file for more details.

Metadata

Release files for ollama-instructor 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ollama-instructor 1.1.0
File Size Uploaded Provenance Actions
ollama_instructor-1.1.0.tar.gz 24.8 kB Without provenance Details for ollama_instructor-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ollama-instructor 1.1.0
File Interpreter ABI Platform Actions
ollama_instructor-1.1.0-py3-none-any.whl Python 3 none any Details for ollama_instructor-1.1.0-py3-none-any.whl

Total release size: 31.3 kB

Release files / ollama_instructor-1.1.0.tar.gz

Download URL ollama_instructor-1.1.0.tar.gz
Size 24.8 kB
Tags Source
SHA-256 checksum
How to use checksums
808aab8293e5799d99faad84843a23b6ea8219fefd3677e2b9ea909cefac8047
BLAKE2b-256 checksum
How to use checksums
f718d0f5f2f7b8dbf1a1d63f847587d4ea3f81e503299ac4b03e216c12dfa8a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.20

Release files / ollama_instructor-1.1.0-py3-none-any.whl

Download URL ollama_instructor-1.1.0-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
98bf05e5d489cc1df0bd2542500895bd6ec1f29fc25a55624191515cff918d1d
BLAKE2b-256 checksum
How to use checksums
6e72155456d15bacdabd8625bd56aac3c549b1de10ca7711163c74b5885cc06b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.20

Security for ollama-instructor 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page