Skip to main content

CLI for OpenAI Structured Output

Project description

ostruct-cli

PyPI version Python Versions Documentation Status CI License: MIT

ostruct tranforms unstructured inputs into structured, usable JSON output using OpenAI APIs.

ostruct will process a set of plain text files (data, source code, CSV, etc), input variables, a dynamic prompt template, and a JSON schema specifying the desired output format, and will produce the result in JSON format.

Features

  • Generate structured JSON output from natural language using OpenAI models and a JSON schema
  • Rich template system for defining prompts (Jinja2-based)
  • Automatic token counting and context window management
  • Streaming support for real-time output
  • Secure handling of sensitive data

Requirements

  • Python 3.10 or higher

Installation

For Users

To install the latest stable version from PyPI:

pip install ostruct-cli

For Developers

If you plan to contribute to the project, see the Development Setup section below for instructions on setting up the development environment with Poetry.

Shell Completion

ostruct-cli supports shell completion for Bash, Zsh, and Fish shells. To enable it:

Bash

Add this to your ~/.bashrc:

eval "$(_OSTRUCT_COMPLETE=bash_source ostruct)"

Zsh

Add this to your ~/.zshrc:

eval "$(_OSTRUCT_COMPLETE=zsh_source ostruct)"

Fish

Add this to your ~/.config/fish/completions/ostruct.fish:

eval (env _OSTRUCT_COMPLETE=fish_source ostruct)

After adding the appropriate line, restart your shell or source the configuration file. Shell completion will help you with:

  • Command options and their arguments
  • File paths for template and schema files
  • Directory paths for -d and --base-dir options
  • And more!

Quick Start

  1. Set your OpenAI API key:
export OPENAI_API_KEY=your-api-key

Example 1: Using stdin (Simplest)

  1. Create a template file extract_person.j2:
Extract information about the person from this text: {{ stdin }}
  1. Create a schema file schema.json:
{
  "type": "object",
  "properties": {
    "person": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string",
          "description": "The person's full name"
        },
        "age": {
          "type": "integer",
          "description": "The person's age"
        },
        "occupation": {
          "type": "string",
          "description": "The person's job or profession"
        }
      },
      "required": ["name", "age", "occupation"],
      "additionalProperties": false
    }
  },
  "required": ["person"],
  "additionalProperties": false
}
  1. Run the CLI:
# Basic usage
echo "John Smith is a 35-year-old software engineer" | ostruct run extract_person.j2 schema.json

# For longer text using heredoc
cat << EOF | ostruct run extract_person.j2 schema.json
John Smith is a 35-year-old software engineer
working at Tech Corp. He has been programming
for over 10 years.
EOF

# With advanced options
echo "John Smith is a 35-year-old software engineer" | \
  ostruct run extract_person.j2 schema.json \
  --model gpt-4o \
  --sys-prompt "Extract precise information about the person" \
  --temperature 0.7

The command will output:

{
  "person": {
    "name": "John Smith",
    "age": 35,
    "occupation": "software engineer"
  }
}

Example 2: Processing a Single File

  1. Create a template file extract_from_file.j2:
Extract information about the person from this text: {{ text.content }}
  1. Use the same schema file schema.json as above.

  2. Run the CLI:

# Basic usage
ostruct run extract_from_file.j2 schema.json -f text input.txt

# With advanced options
ostruct run extract_from_file.j2 schema.json \
  -f text input.txt \
  --model gpt-4o \
  --max-output-tokens 1000 \
  --temperature 0.7

The command will output:

{
  "person": {
    "name": "John Smith",
    "age": 35,
    "occupation": "software engineer"
  }
}

System Prompt Handling

ostruct-cli provides three ways to specify a system prompt, with a clear precedence order:

  1. Command-line option (--sys-prompt or --sys-file):

    # Direct string
    ostruct run template.j2 schema.json --sys-prompt "You are an expert analyst"
    
    # From file
    ostruct run template.j2 schema.json --sys-file system_prompt.txt
    
  2. Template frontmatter:

    ---
    system_prompt: You are an expert analyst
    ---
    Extract information from: {{ text }}
    
  3. Default system prompt (built into the CLI)

Precedence Rules

When multiple system prompts are provided, they are resolved in this order:

  1. Command-line options take highest precedence:

    • If both --sys-prompt and --sys-file are provided, --sys-prompt wins
    • Use --ignore-task-sysprompt to ignore template frontmatter
  2. Template frontmatter is used if:

    • No command-line options are provided
    • --ignore-task-sysprompt is not set
  3. Default system prompt is used only if no other prompts are provided

Example combining multiple sources:

# Command-line prompt will override template frontmatter
ostruct run template.j2 schema.json --sys-prompt "Override prompt"

# Ignore template frontmatter and use default
ostruct run template.j2 schema.json --ignore-task-sysprompt

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

ostruct_cli-0.6.2.tar.gz (84.8 kB view details)

Uploaded Source

Built Distribution

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

ostruct_cli-0.6.2-py3-none-any.whl (102.4 kB view details)

Uploaded Python 3

File details

Details for the file ostruct_cli-0.6.2.tar.gz.

File metadata

  • Download URL: ostruct_cli-0.6.2.tar.gz
  • Upload date:
  • Size: 84.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ostruct_cli-0.6.2.tar.gz
Algorithm Hash digest
SHA256 a8bef031ea0e4442e41891749fca98ac6002d661f2160e288fe080eff34355c5
MD5 435eb70862fdffa7337021b16659b85d
BLAKE2b-256 206ea01a7f684fbbbb1b051f2243146091e4bc6cadc42766e3819ac2e3eb9f7d

See more details on using hashes here.

Provenance

The following attestation bundles were made for ostruct_cli-0.6.2.tar.gz:

Publisher: publish.yml on yaniv-golan/ostruct

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

File details

Details for the file ostruct_cli-0.6.2-py3-none-any.whl.

File metadata

  • Download URL: ostruct_cli-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 102.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ostruct_cli-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0a2efc93579531cf5d29552c8067ae1a5115dea93ce76625893d5b96a27e91d2
MD5 1e46c32ad77582603dc5595122c5377a
BLAKE2b-256 102fb6a654070839d4f9a0bcbabab22aede93f8fc41ace0ad206c804ac8a00a2

See more details on using hashes here.

Provenance

The following attestation bundles were made for ostruct_cli-0.6.2-py3-none-any.whl:

Publisher: publish.yml on yaniv-golan/ostruct

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