CLI for OpenAI Structured Output
Project description
ostruct tranforms unstructured inputs into structured, usable JSON output using OpenAI APIs using dynamic templates
ostruct-cli
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.
Why ostruct?
LLMs are powerful, but getting consistent, structured output from them can be challenging. ostruct solves this problem by providing a streamlined approach to transform unstructured data into reliable JSON structures. The motivation behind creating ostruct was to:
- Bridge the gap between freeform LLM capabilities and structured data needs in production systems
- Simplify integration of AI into existing workflows and applications that expect consistent data formats
- Ensure reliability and validate output against a defined schema to avoid unexpected formats or missing data
- Reduce development time by providing a standardized way to interact with OpenAI models for structured outputs
- Enable non-developers to leverage AI capabilities through a simple CLI interface with templates
Real-World Use Cases
ostruct can be used for various scenarios, including:
Etymology Analysis
ostruct run prompts/task.j2 schemas/etymology.json -f input examples/scientific.txt --model gpt-4o
Break down words into their components, showing their origins, meanings, and hierarchical relationships. Useful for linguistics, educational tools, and understanding terminology in specialized fields.
Automated Code Review
ostruct run prompts/task.j2 schemas/code_review.json -p source "examples/security/*.py" --model gpt-4o
Analyze code for security vulnerabilities, style issues, and performance problems, producing structured reports that can be easily integrated into CI/CD pipelines or developer workflows.
Security Vulnerability Scanning
ostruct run prompts/task.j2 schemas/scan_result.json -d examples/intermediate --model gpt-4o
Scan codebases for security vulnerabilities, combining static analysis with AI-powered reasoning to identify potential issues, suggest fixes, and provide detailed explanations.
Configuration Validation & Analysis
ostruct run prompts/task.j2 schemas/validation_result.json -f dev examples/basic/dev.yaml -f prod examples/basic/prod.yaml
Validate configuration files across environments, check for inconsistencies, and provide intelligent feedback on potential issues or improvements in infrastructure setups.
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
- Model registry management with support for updating to the latest OpenAI models
- Non-intrusive registry update checks with user notifications
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.
Environment Variables
ostruct-cli respects the following environment variables:
OPENAI_API_KEY: Your OpenAI API key (required unless provided via command line)OPENAI_API_BASE: Custom API base URL (optional)OPENAI_API_VERSION: API version to use (optional)OPENAI_API_TYPE: API type (e.g., "azure") (optional)OSTRUCT_DISABLE_UPDATE_CHECKS: Set to "1", "true", or "yes" to disable automatic registry update checks
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
-dand--base-diroptions - And more!
Quick Start
- Set your OpenAI API key:
export OPENAI_API_KEY=your-api-key
Example 1: Using stdin (Simplest)
- Create a template file
extract_person.j2:
Extract information about the person from this text: {{ stdin }}
- 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
}
- 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
- Create a template file
extract_from_file.j2:
Extract information about the person from this text: {{ text.content }}
-
Use the same schema file
schema.jsonas above. -
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:
-
Command-line option (
--sys-promptor--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
-
Template frontmatter:
--- system_prompt: You are an expert analyst --- Extract information from: {{ text }}
-
Default system prompt (built into the CLI)
Precedence Rules
When multiple system prompts are provided, they are resolved in this order:
-
Command-line options take highest precedence:
- If both
--sys-promptand--sys-fileare provided,--sys-promptwins - Use
--ignore-task-syspromptto ignore template frontmatter
- If both
-
Template frontmatter is used if:
- No command-line options are provided
--ignore-task-syspromptis not set
-
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
Model Registry Management
ostruct-cli maintains a registry of OpenAI models and their capabilities, which includes:
- Context window sizes for each model
- Maximum output token limits
- Supported parameters and their constraints
- Model version information
To ensure you're using the latest models and features, you can update the registry:
# Update from the official repository
ostruct update-registry
# Update from a custom URL
ostruct update-registry --url https://example.com/models.yml
# Force an update even if the registry is current
ostruct update-registry --force
This is especially useful when:
- New OpenAI models are released
- Model capabilities or parameters change
- You need to work with custom model configurations
The registry file is stored at ~/.openai_structured/config/models.yml and is automatically referenced when validating model parameters and token limits.
The update command uses HTTP conditional requests (If-Modified-Since headers) to check if the remote registry has changed before downloading, ensuring efficient updates.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ostruct_cli-0.7.1.tar.gz.
File metadata
- Download URL: ostruct_cli-0.7.1.tar.gz
- Upload date:
- Size: 90.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc9137e7c8723e817e27fbe017415536ba4d261868f83a9632891c726929bc4e
|
|
| MD5 |
4a8bab0e31dbc85041c7b9a389e87407
|
|
| BLAKE2b-256 |
97053031b3b3dd8b3622fd66b1f24870c74d2040bd429805457ace8d80e59221
|
Provenance
The following attestation bundles were made for ostruct_cli-0.7.1.tar.gz:
Publisher:
publish.yml on yaniv-golan/ostruct
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ostruct_cli-0.7.1.tar.gz -
Subject digest:
dc9137e7c8723e817e27fbe017415536ba4d261868f83a9632891c726929bc4e - Sigstore transparency entry: 182859377
- Sigstore integration time:
-
Permalink:
yaniv-golan/ostruct@7d1470ee6fc9f4893ed390d602812c5f8d4c9fd2 -
Branch / Tag:
refs/tags/v0.7.1 - Owner: https://github.com/yaniv-golan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@7d1470ee6fc9f4893ed390d602812c5f8d4c9fd2 -
Trigger Event:
release
-
Statement type:
File details
Details for the file ostruct_cli-0.7.1-py3-none-any.whl.
File metadata
- Download URL: ostruct_cli-0.7.1-py3-none-any.whl
- Upload date:
- Size: 106.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
676146b31fbf0384b013393db37491ff14e0f61e102b423f9161a68ff27399cf
|
|
| MD5 |
de51b79ace5d8c08bc336289aca131a9
|
|
| BLAKE2b-256 |
cda7bcd6a680e267c649d981044c984bbfc644e418c5dbeff6962e8925a3b9db
|
Provenance
The following attestation bundles were made for ostruct_cli-0.7.1-py3-none-any.whl:
Publisher:
publish.yml on yaniv-golan/ostruct
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ostruct_cli-0.7.1-py3-none-any.whl -
Subject digest:
676146b31fbf0384b013393db37491ff14e0f61e102b423f9161a68ff27399cf - Sigstore transparency entry: 182859379
- Sigstore integration time:
-
Permalink:
yaniv-golan/ostruct@7d1470ee6fc9f4893ed390d602812c5f8d4c9fd2 -
Branch / Tag:
refs/tags/v0.7.1 - Owner: https://github.com/yaniv-golan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@7d1470ee6fc9f4893ed390d602812c5f8d4c9fd2 -
Trigger Event:
release
-
Statement type: