Skip to main content

A minimal DSPy-inspired library with native OpenAI tool calling

Project description

udspy

PyPI version Python versions Tests Documentation codecov License: MIT

A lightweight DSPy-inspired library optimized for resource-constrained environments, with native OpenAI tool calling, human-in-the-loop workflows, and conversation history.

Topics: python openai llm dspy pydantic async ai-framework function-calling tool-calling streaming conversational-ai prompt-engineering type-hints pytest chatbot agent human-in-the-loop

About This Project

DSPy is a fantastic, production-ready framework with many more features and a thriving ecosystem. If you're looking for a comprehensive solution with extensive provider support, optimizers, and advanced features, DSPy is the recommended choice.

This project exists to address a specific use case: resource-constrained environments. DSPy's dependency on LiteLLM (which requires ~200MB of memory when loaded) makes it challenging to use in contexts with limited resources, such as:

  • Serverless functions with memory limits
  • Edge deployments
  • Embedded systems
  • Cost-sensitive cloud environments

udspy takes the excellent developer experience and core concepts from DSPy (for which we're deeply grateful) and provides:

  • Minimal footprint: Uses the OpenAI library directly (~10MB vs ~200MB)
  • OpenAI-compatible providers: Works with any provider compatible with OpenAI's API (OpenAI, Azure OpenAI, Together AI, Groq, etc.)
  • Additional features for common patterns:
    • Human-in-the-loop workflows with state management
    • Automatic tool calling with multi-turn conversations
    • Built-in conversation history management
    • Interruptible tools for user confirmation

The core abstractions (Signatures, Modules, Predictions) are heavily inspired by DSPy's elegant design. If resource constraints aren't a concern for your use case, we strongly encourage you to check out DSPy for a more feature-complete solution.

Features

  • Pydantic-based Signatures: Define inputs, outputs, and tools using Pydantic models
  • Human-in-the-Loop Workflows: Built-in interrupt system for user confirmation, clarification, and feedback
    • @interruptible decorator for function-level interruption
    • Thread-safe and asyncio task-safe state management
    • Support for approval, rejection, argument modification, and feedback
  • Automatic Tool Calling: Use @tool decorator for automatic tool execution with multi-turn conversations
  • ReAct Agent: Reasoning and acting agent with tool calling and self-reflection
  • Conversation History: Built-in History class for managing multi-turn conversations
  • Optional Tool Execution: Control whether tools execute automatically or return for manual handling
  • Module Abstraction: Compose LLM calls with reusable modules (Predict, ChainOfThought, ReAct)
  • Streaming Support: Stream reasoning and output fields incrementally with async generators
  • Minimal Dependencies: Only requires openai and pydantic (~10MB total footprint)

Installation

For Development

# Clone the repository
git clone https://github.com/silvestrid/udspy
cd udspy

# Install dependencies and package in editable mode
uv sync
uv pip install -e .

# Or with pip
pip install -e .

For Users

# When published to PyPI
pip install udspy

# Or with uv
uv pip install udspy

Quick Start

Basic Usage

import udspy
from udspy import Signature, InputField, OutputField, Predict

# Configure OpenAI client
udspy.settings.configure(api_key="your-api-key", model="gpt-4o-mini")

# Define a signature
class QA(Signature):
    """Answer questions concisely."""
    question: str = InputField()
    answer: str = OutputField()

# Create and use a predictor
predictor = Predict(QA)
result = predictor(question="What is the capital of France?")
print(result.answer)

With Conversation History

from udspy import History

predictor = Predict(QA)
history = History()

# Multi-turn conversation
result = predictor(question="What is Python?", history=history)
print(result.answer)

result = predictor(question="What are its main features?", history=history)
print(result.answer)  # Context from previous turn is maintained

With Automatic Tool Calling

from udspy import tool
from pydantic import Field

@tool(name="Calculator", description="Perform arithmetic operations")
def calculator(
    operation: str = Field(description="add, subtract, multiply, divide"),
    a: float = Field(description="First number"),
    b: float = Field(description="Second number"),
) -> float:
    ops = {"add": a + b, "subtract": a - b, "multiply": a * b, "divide": a / b}
    return ops[operation]

predictor = Predict(QA, tools=[calculator])
result = predictor(question="What is 157 times 234?")
print(result.answer)  # Tools are automatically executed

With Human-in-the-Loop

from udspy import ReAct, HumanInTheLoopRequired, tool
from pydantic import Field
import os

@tool(
    name="delete_file",
    description="Delete a file",
    interruptible=True  # Requires user confirmation
)
def delete_file(path: str = Field(description="File path")) -> str:
    os.remove(path)
    return f"Deleted {path}"

class FileTask(Signature):
    """Perform file operations safely."""
    request: str = InputField()
    result: str = OutputField()

agent = ReAct(FileTask, tools=[delete_file])

try:
    result = agent(request="Delete /tmp/old_data.txt")
except HumanInTheLoopRequired as e:
    print(f"Agent asks: {e.question}")
    # User confirms: "yes", "no", or provides feedback
    result = agent.resume("yes", e)
    print(result.result)

Development

# Install dependencies and package in editable mode
just install
uv pip install -e .

# Run tests
just test

# Run linter
just lint

# Format code
just fmt

# Type check
just typecheck

# Run all checks
just check

# Build docs
just docs-serve

Documentation

Full documentation is available at silvestrid.github.io/udspy

Or browse locally:

Building Documentation

# Install mkdocs dependencies
pip install mkdocs-material mkdocstrings[python]

# Serve docs locally
mkdocs serve

# Build static site
mkdocs build

Contributing

See CONTRIBUTING.md for development setup and guidelines.

Releases

Releases are automated via GitHub Actions:

  1. Update version in pyproject.toml and src/udspy/__init__.py
  2. Commit and tag: git tag v0.x.x && git push --tags
  3. GitHub Actions will build, test, and publish to PyPI
  4. Documentation will be deployed to GitHub Pages

See CONTRIBUTING.md for detailed release instructions.

License

MIT

Project details


Download files

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

Source Distribution

udspy-0.1.0.tar.gz (167.4 kB view details)

Uploaded Source

Built Distribution

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

udspy-0.1.0-py3-none-any.whl (36.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: udspy-0.1.0.tar.gz
  • Upload date:
  • Size: 167.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for udspy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 708b001e6cac647aaca9c025b11cd1cf49da8a87cf03627a87913f499c3dbac2
MD5 d21be38a6e2021cba8633fe0d2c58ce2
BLAKE2b-256 614b2d85204b18ba9ddc555c4d262d426f829b0d28535c71025a6d7fb3a2863d

See more details on using hashes here.

Provenance

The following attestation bundles were made for udspy-0.1.0.tar.gz:

Publisher: release.yml on silvestrid/udspy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: udspy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 36.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for udspy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 86534e3dc44a261fad7cd4f09349c3ab254748ab45d3e54f159855b5ca42b291
MD5 8208723b4663a231b2c2552021adbda3
BLAKE2b-256 8e48f7680e240f14d0429b9d34e7bc421c0d62e79e6ebf094daa36036b382f2d

See more details on using hashes here.

Provenance

The following attestation bundles were made for udspy-0.1.0-py3-none-any.whl:

Publisher: release.yml on silvestrid/udspy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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