Skip to main content

Structure Outputs from Language Models

Project description

Outformer Logo Outformer Logo

Outformer: Structure Outputs from Language Models

PyPI - Package Version Python Versions License Docs - GitHub.io

Outformer is a powerful library that enables language models to generate structured outputs. It ensures always valid JSON outputs by generating only values while maintaining the structural integrity of your schema.

Features

  • 🔄 Structured Output Generation: Generate valid JSON outputs from language models
  • 🎯 Schema Validation: Ensure outputs conform to your JSON schema
  • 🛠️ Flexible Integration: Works with any Hugging Face transformer model
  • 🚀 Easy to Use: Simple API with minimal configuration
  • 🎨 Value Highlighting: Visualize generated values in your JSON structure

Installation

We recommend Python 3.10+, PyTorch 2.7.0+, transformers v4.51.3+.

Install via pip

pip install outformer

Install from source

git clone https://github.com/milistu/outformer.git
cd outformer
pip install -e .

Quick Start

Here's a simple example to get you started:

Quick Start Example Quick Start Example

Click to expand code example
from outformer import Jsonformer, highlight_values
from transformers import AutoModelForCausalLM, AutoTokenizer

# Initialize model and tokenizer
model_name = "Qwen/Qwen3-0.6B"
model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Create Jsonformer instance
jsonformer = Jsonformer(model, tokenizer, max_tokens_string=100)

# Define your JSON schema
json_schema = {
    "type": "object",
    "properties": {
        "brand": {
            "type": "string",
            "description": "Brand of the product",
        },
        "model": {
            "type": "string",
            "description": "Model of the product",
        },
        "product_type": {
            "type": "string",
            "description": "Type of the product",
        },
        "gender": {
            "type": "string",
            "enum": ["Female", "Male", "Unisex"],
        },
        "color": {
            "type": "string",
            "description": "Color of the product",
        },
        "features": {
            "type": "array",
            "minItems": 3,
            "items": {
                "type": "string",
                "description": "Features of the product that may be relevant for the customer",
            },
        },
    },
}

# Your input prompt
prompt = """
Extract key information from the product description:

adidas Men's Powerlift.3 Cross-Trainer Shoes
A powerful shoe with lockdown fit. Made with an extra-wide design that allows the foot to spread, these men's lifting/weight-training shoes pair a snug-fitting upper with a wide midfoot strap for extra support. A high-density die-cut wedge midsole keeps you close to the ground.
100% Synthetic leather
Imported
Rubber sole
Removable Insole
"""

# Generate structured output
generated_data = jsonformer.generate(schema=json_schema, prompt=prompt)

# Highlight generated values
highlight_values(generated_data)

The code above will generate a structured JSON output and display it with highlighted values. Here's what you'll get:

{
    "brand": "Adidas",
    "model": "Powerlift.3",
    "product_type": "Lifting/Weight Training Shoes",
    "gender": "Male",
    "color": "Black",
    "features": [
        "Extra wide design for optimal foot support",
        "High-density die-cut wedge midsole",
        "Rubber sole with removable insole",
        "100% synthetic leather"
    ]
}

When using highlight_values(), the output will be displayed in your terminal with the generated values highlighted in color, making it easy to distinguish between the structure and the generated content.

Advanced Usage

Configuration Options

The Jsonformer class accepts several configuration parameters:

  • debug (bool): Enable debug mode for detailed generation process
  • max_array_length (int): Maximum number of elements in an array
  • max_tokens_number (int): Maximum number of tokens for number generation
  • max_tokens_string (int): Maximum number of tokens for string generation
  • temperature (float): Sampling temperature for generation
  • generation_marker (str): Marker for tracking generation position
  • max_attempts (int): Maximum attempts for value generation

Supported JSON Schema Features

  • Basic types: string, number, boolean
  • Arrays with min/max items
  • Objects with nested properties
  • Enums for constrained string values
  • Descriptions for better generation context

Contributing

We welcome contributions! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

License

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

Citing & Authors

The idea for this repository was inspired by jsonformer.

Maintainer: Milutin Studen

Support

If you encounter any issues or have questions, please open an issue on our GitHub repository.

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

outformer-0.1.1.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

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

outformer-0.1.1-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file outformer-0.1.1.tar.gz.

File metadata

  • Download URL: outformer-0.1.1.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.10.13 Darwin/24.5.0

File hashes

Hashes for outformer-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f34361bb68fae30cfaf446512654c8a513137aa1ab3f2dd532bb33b2adad519c
MD5 7a076bfe800477a0733c9d0c5948ef30
BLAKE2b-256 30638c0f086bae932bc7076c2a439c3667996c220deb9612ea08a43ace899661

See more details on using hashes here.

File details

Details for the file outformer-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: outformer-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.10.13 Darwin/24.5.0

File hashes

Hashes for outformer-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 89e18cf7062761ca9b42a55f691273df316e5c977924910126bbfd539b58f796
MD5 f480a5d3dfed9c88e83bcf910fae3b87
BLAKE2b-256 ec826d21cdc40a6e6ca774215ab395817d97182e7af5665daf53c046fe209432

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