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 formatmodel: OpenAI model name (e.g., "gpt-4.1-mini")tooling: OptionalToolingimplementation for function callingschema: Optional dictionary defining the output schemamemory: Optional vector store ID for file searchtimeout: Request timeout in seconds (default: 30)
Tooling Protocol
Protocol for implementing function calling:
on_function_call(self, name: str, args: dict): Handle function calls, returnResultto continue or any other value to return immediatelyget_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
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 bfg_llmutil-0.5.1.tar.gz.
File metadata
- Download URL: bfg_llmutil-0.5.1.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c28a45779ba83421091fb40192742449930b6e7a27ed76c9ab74d1f187a3755a
|
|
| MD5 |
64a7ba3085612188227fe2deb1404b15
|
|
| BLAKE2b-256 |
ed78e53356a779565b6c620735b362b379470570b0c09853abe439feb10ab0e4
|
File details
Details for the file bfg_llmutil-0.5.1-py3-none-any.whl.
File metadata
- Download URL: bfg_llmutil-0.5.1-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd7126b35eeeae44c197db5ed397bd0f6a832e19ab699f45dd4416aa2735675c
|
|
| MD5 |
7f6a3cbff2412997b5146452fbd206d8
|
|
| BLAKE2b-256 |
0d8a2e18f6a02875fcff63c8274faf91f26013ca7edf2c738874dca4a5038a8b
|