Skip to main content

A Python library for structured LLM development with schema validation

Project description

LangTask

License: MIT Python Version Status

LangTask is a lightweight Python library for rapidly setting up and managing LLM prompts with structured input/output validation. It provides a clean, type-safe interface for working with language models while enforcing schema validation and proper error handling.

⚠️ Note: This is a pre-alpha, work-in-progress project used internally for client work. It is not yet stable for production use and the API may change significantly.

Features

  • 🔍 Schema Validation: Type-safe input and output validation using Pydantic models
  • 🔄 Provider Flexibility: Support for multiple LLM providers (currently OpenAI and Anthropic)
  • 📝 Prompt Management: Simple directory-based prompt organization and discovery
  • Easy Integration: Clean API for registering and running prompts
  • 🛠️ Error Handling: Comprehensive error hierarchy with detailed feedback
  • 📊 Logging: Structured logging with request tracing and performance monitoring

Installation

pip install langtask

Quick Start

First, import the library (we'll use this import in all following examples):

import langtask as lt
  1. Create a prompt directory structure:
prompts/
└── greeting/
    ├── config.yaml           # LLM configuration
    ├── instructions.md       # Prompt template
    ├── input_schema.yaml     # Input validation schema
    └── output_schema.yaml    # Output validation schema (optional)
  1. Configure your prompt:
# config.yaml
id: greeting
display_name: "Greeting Generator"
description: "Generates personalized greetings"
llm:
  provider: "anthropic"
  model: "claude-3-5-sonnet-20241022"
  temperature: 0.7
  1. Define input schema:
# input_schema.yaml
name:
  type: string
  description: "Name of the person to greet"
style:
  type: string
  description: "Style of greeting (formal/casual)"
  enum: ["formal", "casual"]
  1. Create prompt instructions:
# Note: Variable names are case-insensitive
Generate a {{STYLE}} greeting for {{Name}}.
# Will work the same as:
Generate a {{style}} greeting for {{name}}.
  1. Use in your code:
# Register prompt directory
lt.register("./prompts")

# Generate text - variable names are case-insensitive
response = lt.run("greeting", {
    "NAME": "Alice",  # Will work
    "style": "casual" # Will also work
})

print(response)  # "Hey Alice! How's it going?"

Variable Naming

LangTask handles variable names case-insensitively throughout the system:

  • Template variables like {{NAME}}, {{name}}, or {{Name}} are treated as identical
  • Input parameters can use any case (e.g., "NAME", "name", "Name")
  • Schema definitions use lowercase internally
  • All comparisons and validations are case-insensitive

This makes the system more flexible and less error-prone when dealing with variable names.

Example Prompt Structure

LangTask uses a directory-based approach for organizing prompts:

prompts/
├── greeting/
│   ├── config.yaml
│   ├── instructions.md
│   └── input_schema.yaml
└── sentiment/
    ├── config.yaml
    ├── instructions.md
    ├── input_schema.yaml
    └── output_schema.yaml

Each prompt requires:

  • config.yaml: LLM provider settings and prompt metadata
  • instructions.md: The actual prompt template with variable placeholders
  • input_schema.yaml: Schema defining expected input parameters
  • output_schema.yaml: (Optional) Schema for structured output validation

Configuration

Set global defaults for all prompts:

lt.set_global_config({
    "provider": "anthropic",
    "model": "claude-3-opus-20240229",
    "temperature": 0.1
})

Or use provider-specific settings per prompt in config.yaml:

llm:
  - provider: "anthropic"
    model: "claude-3-opus-20240229"
    temperature: 0.7
  - provider: "openai"
    model: "gpt-4"
    temperature: 0.5
    max_tokens: 1000

Logging

LangTask provides comprehensive logging with configurable settings for both console and file output:

Features

  • Colored console output with configurable level
  • File logging with automatic rotation
  • Request ID tracking for operation tracing
  • Performance metrics for monitoring
  • Structured formatting for easy parsing

Configuration

Use set_logs() to configure logging behavior:

# Basic usage - just set log directory
lt.set_logs("./my_logs")

# Detailed configuration
lt.set_logs(
    path="./app/logs",              # Custom log directory
    console_level="WARNING",        # Less console output
    file_level="DEBUG",            # Detailed file logs
    rotation="100 MB",             # Larger log files
    retention="1 month"            # Keep logs longer
)

# Reset to defaults (logs/ directory with standard settings)
lt.set_logs()

Configuration Options

  • path: Directory for log files (default: './logs')
  • console_level: Console logging level (default: 'INFO')
    • Options: 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'
  • file_level: File logging level (default: 'DEBUG')
    • Options: Same as console_level
  • rotation: When to rotate log files (default: '10 MB')
    • Size-based: '10 MB', '1 GB', etc.
    • Time-based: '1 day', '1 week', etc.
  • retention: How long to keep old logs (default: '1 week')
    • '1 week', '1 month', '90 days', etc.

Default Behavior

  • Console: INFO level with color-coded output
  • File: DEBUG level for detailed troubleshooting
  • Location: ./logs/langtask.log
  • Rotation: 10 MB file size
  • Retention: 1 week

Fallback Behavior

If the specified log directory cannot be created or accessed:

  • Custom path: Raises FileSystemError
  • Default path: Falls back to console-only logging with warning

Example Log Output

2024-03-22 10:15:30 | req-123 | INFO    | prompt_loader  | Loading prompt | prompt_id=greeting
2024-03-22 10:15:30 | req-123 | WARNING | schema_loader  | Unknown field detected | field=custom_param
2024-03-22 10:15:31 | req-123 | SUCCESS | llm_processor  | Request processed | duration_ms=523.45

Environment Setup

LangTask supports multiple ways to configure your API keys:

  1. Direct environment variables:
# For Anthropic
export ANTHROPIC_API_KEY=sk-ant-...

# For OpenAI
export OPENAI_API_KEY=sk-...
  1. Using .env file (recommended for development):
# .env
ANTHROPIC_API_KEY=sk-ant-...
OPENAI_API_KEY=sk-...

Then in your code:

from dotenv import load_dotenv
load_dotenv()
  1. Setting in your deployment environment (recommended for production)

Remember to add .env to your .gitignore to protect your API keys.

Development Status

This project is in pre-alpha stage and is actively being developed. Current limitations:

  • API may change without notice
  • Limited provider support (OpenAI and Anthropic only)
  • Basic error recovery
  • Limited documentation

Requirements

  • Python 3.10 or higher
  • Dependencies:
    • pydantic >= 2.0
    • langchain >= 0.1.0
    • langchain-openai >= 0.0.2
    • langchain-anthropic >= 0.1.1
    • pyyaml >= 6.0
    • python-dotenv >= 0.19.0
    • loguru >= 0.7.0

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

While contributions are welcome, please note that this project is in pre-alpha stage with frequent breaking changes. If you'd like to contribute, please open an issue first to discuss what you would like to change.

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

langtask-0.1.0.tar.gz (37.2 kB view details)

Uploaded Source

Built Distribution

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

langtask-0.1.0-py3-none-any.whl (42.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: langtask-0.1.0.tar.gz
  • Upload date:
  • Size: 37.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for langtask-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6ed8e0171f6de3ca9fa353eed03c5ae89e4c64a66e7fa207665af3b6bbbd8bd3
MD5 94dd77adba99496fed5301e178329f02
BLAKE2b-256 f49bbe03e0e06ffa2e8f843202b60de0008de81f845959ae9bee7b77d4d019f6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: langtask-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 42.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for langtask-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 85a474269ddea39acd2cc8d3e914ea522286bfaf4b2b1638ec79a8115054342f
MD5 11ff2bd9d1458f2ac9fdd1cd86f7a65c
BLAKE2b-256 30dcc4512b262c1c36822cb407fd4cb263e59840b1af1f8834443c1ceb6d4373

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 Pingdom Monitoring Sentry Error logging StatusPage Status page