It is, and you have pointed out a valid issue with playground’s features. I assume that “functions” is still wired up to their deprecated “function_call” endpoint.
If you use a notebook and call the API directly and you won’t be able to reproduce the schema injection bugs. If you want to test the structured outputs in the playground you must use the response_format
instead (at least until they fix playground).
You can copy and paste your schema into the json_schema
input window and the linter will throw the warnings for you.
Here is the test I ran in the notebook with different seeds for each call; no validation issues and no extra args:
import openai, random, pydantic
from tooldantic import ModelBuilder, ToolBaseModel, OpenAiStrictSchemaGenerator
class NoExtraArgsStrictModel(ToolBaseModel):
_schema_generator = OpenAiStrictSchemaGenerator
model_config = {'extra': 'forbid'}
model_builder = ModelBuilder(base_model=NoExtraArgsStrictModel)
Model = model_builder.model_from_json_schema(
{
"name": "record_availability",
"description": "Record excess capacity a carrier has available",
"parameters": {
"type": "object",
"required": ["routes", "offered_on_date"],
"properties": {
"routes": {
"type": "string",
"description": 'JSON array, conforming to the type\n\nArray<{\n carrier: string; // the name of the carrier\n available_on: string | null; // a date formatted as "MM/DD/YYYY"\n\n // A city name, such as "New York, NY, USA". If there are multiple possible origins, separate them by "/", such as "New York, NY, USA / San Francisco, CA, USA / Chicago, IL, USA".\n origin: string; \n\n // if the carrier describes the origin as within a certain distance from a city, put the radius here\n // for example if they say "within 50 miles of Chicago, IL" put "50mi" \n origin_radius: string | null;\n\n // A city name, such as "New York, NY, USA". If there are multiple possible destinations, separate them by "/", such as "New York, NY, USA / San Francisco, CA, USA / Chicago, IL, USA".\n destination: string | null;\n\n // if the carrier describes the destination as within a certain distance from a city, put the radius here\n // for example if they say "within 50 miles of Chicago, IL" put "50mi" \n destination_radius: string | null;\n\n truck_type: string; // The type of truck. If none is provided, assume\n}>',
},
"offered_on_date": {
"type": "string",
"description": 'the date the carrier sent the email with the availabilities. Format as "MM/DD/YYYY"',
},
},
},
}
)
sys = """
We are a freight forwarding company. From time to time we receive emails from carriers, describing excess capacity they have available. Following is an email thread we have received. If it has information about route availability, call the record_availability function with appropriate arguments.
Today is Tuesday, August 20, 2024 and the time is currently 5:38:58 PM EDT.
Use the tools available to you to resolve the email thread below:
"""
user = """
Subject: Availability Notification
Date: Thu, Aug 15, 2024 at 5:08 PM
Hello,
I am sharing the availability for today and tomorrow:
- 1 unit available now in Springfield/Oakland, Chicago, or nearby areas, heading to Arizona or directly within Mexico.
- 1 unit available now in Mexico City, Querétaro, Guanajuato, or nearby areas, heading to Torreón, Laredo, or directly within the USA.
If you have any loads in these areas, we would be happy to review them.
Best regards,
John Smith
"""
def call_llm(seed):
r = openai.OpenAI().chat.completions.create(
model='gpt-4o-mini',
messages=[
{"role": "system", "content": sys},
{"role": "user", "content": user}
],
tools=[Model.model_json_schema()],
tool_choice='required',
parallel_tool_calls=False,
seed=seed
)
message = r.choices[0].message
tc = message.tool_calls[0]
try:
validated_data = Model.model_validate_json(tc.function.arguments)
print(validated_data)
except pydantic.ValidationError as e:
print(e.errors())
for i in random.sample(range(100), 10):
call_llm(i)
# INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
# routes='1 unit available now in Springfield/Oakland, Chicago, or nearby areas, heading to Arizona or directly within Mexico. 1 unit available now in Mexico City, Querétaro, Guanajuato, or nearby areas, heading to Torreón, Laredo, or directly within the USA.' offered_on_date='08/15/2024'
# INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
# routes='1 unit available now in Springfield/Oakland, Chicago, or nearby areas, heading to Arizona or directly within Mexico.' offered_on_date='08/15/2024'
# INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
# routes='1 unit available now in Springfield/Oakland, Chicago, or nearby areas, heading to Arizona or directly within Mexico.' offered_on_date='08/15/2024'
# INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
# routes='1 unit available now in Springfield/Oakland, Chicago, or nearby areas, heading to Arizona or directly within Mexico. / 1 unit available now in Mexico City, Querétaro, Guanajuato, or nearby areas, heading to Torreón, Laredo, or directly within the USA.' offered_on_date='08/15/2024'
# INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
# routes='1 unit available now in Springfield/Oakland, Chicago, or nearby areas, heading to Arizona or directly within Mexico. / 1 unit available now in Mexico City, Querétaro, Guanajuato, or nearby areas, heading to Torreón, Laredo, or directly within the USA.' offered_on_date='08/15/2024'
# INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
# routes='1 unit available now in Springfield/Oakland, Chicago, or nearby areas, heading to Arizona or directly within Mexico.' offered_on_date='08/15/2024'
# INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
# routes='Springfield/Oakland, Chicago to Arizona/Mexico' offered_on_date='08/15/2024'
# INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
# routes='1 unit available now in Springfield/Oakland, Chicago, or nearby areas, heading to Arizona or directly within Mexico.' offered_on_date='08/15/2024'
# INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
# routes='Springfield/Oakland, Chicago, or nearby areas to Arizona / Mexico' offered_on_date='08/15/2024'
# INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
# routes='Springfield/Oakland, Chicago, or nearby areas to Arizona or directly within Mexico' offered_on_date='08/15/2024'