Skip to main content

A set of utility functions to handle LLM structural output

Project description

llmutil

This library provides tools to generate structured output and function calling from the OpenAI API.

What is Structured Output?

Structured Output is the recommended method for getting formatted responses. It guarantees that outputs follow your defined schema, making it more reliable than previous methods like JSON mode, function calls, or tool use.

How to Use

Structured Output

To use Structured Output, you need to define a schema using a simple dictionary format:

from llmutil import new_response

output = new_response(
    [
        {
            "role": "user",
            "content": "normalize this address: 1 hacker way, menlo park, california",
        }
    ],
    model="gpt-4.1-mini",
    schema={
        "street": "string",
        "city": "string",
        "state": "string",
    },
)
# {'street': '1 Hacker Way', 'city': 'Menlo Park', 'state': 'CA'}

Function Calling

For function calling, implement the Tooling protocol to define available functions:

from llmutil import Result, Tooling, new_response

def add(a, b):
    return a + b

class UseAdd(Tooling):
    def on_function_call(self, name, args):
        if name == "add":
            return Result(add(args["a"], args["b"]))

    def get_tools(self):
        return {
            "add": {
                "a": "number",
                "b": "number",
            }
        }

messages = [
    {
        "role": "system",
        "content": "you cannot do math. you must use the add() function to add numbers.",
    },
    {
        "role": "user",
        "content": "alice has 10 apples, bob has 20 apples, how many apples do they have in total?",
    },
]

output = new_response(messages, model="gpt-4.1-mini", tooling=UseAdd())
# Alice and Bob have 30 apples in total.

API Reference

new_response(messages, *, model, tooling=None, schema=None, memory=None, timeout=30)

Main function for generating responses from OpenAI API.

  • messages: List of message dictionaries in OpenAI format
  • model: OpenAI model name (e.g., "gpt-4.1-mini")
  • tooling: Optional Tooling implementation for function calling
  • schema: Optional dictionary defining the output schema
  • memory: Optional vector store ID for file search
  • timeout: Request timeout in seconds (default: 30)

Tooling Protocol

Protocol for implementing function calling:

  • on_function_call(self, name: str, args: dict): Handle function calls, return Result to continue or any other value to return immediately
  • get_tools(self): Return dictionary mapping function names to parameter schemas

Result Class

Wrapper for function call results that should continue the conversation:

  • Result(result): Wrap a result to continue the conversation flow

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

bfg_llmutil-0.5.5.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

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

bfg_llmutil-0.5.5-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file bfg_llmutil-0.5.5.tar.gz.

File metadata

  • Download URL: bfg_llmutil-0.5.5.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.9

File hashes

Hashes for bfg_llmutil-0.5.5.tar.gz
Algorithm Hash digest
SHA256 201cb9f2275e0273295ef309f275477002e460be5c29e861a4e5e7dbb8fbb897
MD5 2b621c631ec2f90888416cf27d996266
BLAKE2b-256 00ba4ce582dc7e2d2ba5455d5db5e4ffb0a22fa1e582833401d34a771c220fd7

See more details on using hashes here.

File details

Details for the file bfg_llmutil-0.5.5-py3-none-any.whl.

File metadata

File hashes

Hashes for bfg_llmutil-0.5.5-py3-none-any.whl
Algorithm Hash digest
SHA256 867f28db8fa1ef16701bdd2d214de1765d136a17098948cd7f7fbc62679b7074
MD5 8479293bb89613df5956ffdf1cdc21a0
BLAKE2b-256 5f8b5e72cd894e08d636bd8350ff4b468f32be7f53c34918a83b03a318f5ab56

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