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 calls 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. This library makes it easy with simple type definitions:

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",
    },
)

# {'type': 'message', 'content': {'street': '1 Hacker Way', 'city': 'Menlo Park', 'state': 'CA'}}
print(output["content"])

Function Calls

You can also use function calls to give the model access to tools:

from llmutil import new_response, build_function_call_messages

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

tools = {
    "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", tools=tools)

# {'type': 'function_call', 'name': 'add', 'args': {'a': 10, 'b': 20}}
print(output)

# Execute the function and continue the conversation
output["result"] = add(output["args"]["a"], output["args"]["b"])
output = new_response(
    messages + build_function_call_messages(output),
    model="gpt-4.1-mini",
    tools=tools,
)

# {'type': 'message', 'content': 'Alice and Bob have 30 apples in total.'}
print(output)

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.0.tar.gz (9.1 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.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for bfg_llmutil-0.5.0.tar.gz
Algorithm Hash digest
SHA256 ebc6df4f88bb54727a41ae17f3b9a3b4e3abf047cf197d4cf8d0e8e372b995ea
MD5 6472d96afd8ff742aaed839175852486
BLAKE2b-256 310a29a564ff6e2834aad1b840017f0e9753f07323f27880a34dff75a85b6404

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bfg_llmutil-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8b0aa09d8fb6b680992d8d72e86134560c2ce69b52a1c4769246246c42384c89
MD5 10520311ea2e0be8eee2b0425999ae98
BLAKE2b-256 97ab0ea2443a7463f949457e361d0b66361272c76908595e04ec267c33eb38e0

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