CLI for OpenAI Structured Output
Project description
ostruct-cli
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
-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
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.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8bef031ea0e4442e41891749fca98ac6002d661f2160e288fe080eff34355c5
|
|
| MD5 |
435eb70862fdffa7337021b16659b85d
|
|
| BLAKE2b-256 |
206ea01a7f684fbbbb1b051f2243146091e4bc6cadc42766e3819ac2e3eb9f7d
|
Provenance
The following attestation bundles were made for ostruct_cli-0.6.2.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.6.2.tar.gz -
Subject digest:
a8bef031ea0e4442e41891749fca98ac6002d661f2160e288fe080eff34355c5 - Sigstore transparency entry: 173483752
- Sigstore integration time:
-
Permalink:
yaniv-golan/ostruct@a9a7bd98a4ca3735bd6c398ee662198e4ccc2b02 -
Branch / Tag:
refs/tags/v0.6.2 - Owner: https://github.com/yaniv-golan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a9a7bd98a4ca3735bd6c398ee662198e4ccc2b02 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a2efc93579531cf5d29552c8067ae1a5115dea93ce76625893d5b96a27e91d2
|
|
| MD5 |
1e46c32ad77582603dc5595122c5377a
|
|
| BLAKE2b-256 |
102fb6a654070839d4f9a0bcbabab22aede93f8fc41ace0ad206c804ac8a00a2
|
Provenance
The following attestation bundles were made for ostruct_cli-0.6.2-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.6.2-py3-none-any.whl -
Subject digest:
0a2efc93579531cf5d29552c8067ae1a5115dea93ce76625893d5b96a27e91d2 - Sigstore transparency entry: 173483754
- Sigstore integration time:
-
Permalink:
yaniv-golan/ostruct@a9a7bd98a4ca3735bd6c398ee662198e4ccc2b02 -
Branch / Tag:
refs/tags/v0.6.2 - Owner: https://github.com/yaniv-golan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a9a7bd98a4ca3735bd6c398ee662198e4ccc2b02 -
Trigger Event:
release
-
Statement type: