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.2.tar.gz (9.5 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.2-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for bfg_llmutil-0.5.2.tar.gz
Algorithm Hash digest
SHA256 f67b58c0130c8fa417e7c32a40ca40b04517ad454eabbaa1705377197e197af4
MD5 d892d5462e836aa078bd8a39e5288f15
BLAKE2b-256 383550472af67b98ba1f81d32505bcc559bb16af28d4f534f72ab371282151db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bfg_llmutil-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ee9e76df8ccbde778347229dd48da8f84c745b01c9c438ae125514d358776fb4
MD5 8012a82909adf88c6e95c826ae693e0e
BLAKE2b-256 88058365bdf488cd2795d6ff46c5e8fad84d4bb138cc1d137023a7a6ac84f984

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