Interestingly, this new approach takes a class as a parameter instead of a function. While it’s possible to describe a function using a class (with class members understood as parameters), this approach feels unusual.
I didn’t really bother to look into it but I imagine that the first is applicable only for function calling, while a schema/class is applicable for both (function calling & json schemas)
This is part of the beta SDK method for passing a Pydantic BaseModel class object into the SDK, instead a streamable Python data object, and having it create a validation schema.
This does the work of adding everything required by “strict” for you. No forgetting a closing brace or an additionalProperties: false. Your class can be used to validate the AI generation. You can have just one definition used throughout code.
When a BaseModel is used with response_format, the beta SDK described in helpers.md adds a parsed element to the chat completions user response, that validates and does additional work in making the values directly accessible by methods when you use .parse().
I personally “just say no”, maximizing portability across vendors.
The input is not just any old class, where a class has attributes and can be built to fit a need. It is a Pydantic BaseModel. Pydantic already has lots of support for schemas and validation. It is built for structured data, possibly containing many models, while a general function, or method, is more oriented for the doing. The SDK is specifically built to take Pydantic. Basically a switch - is input dict or BaseModel. It is just more realized when you look at how response_format is utilized.
Pydantic’s BaseModel provides several built-in methods that you can use to work with your data models. Here are a few key ones:
Parsing and Validation:
model_validate(): Parses and validates data from a dictionary or object.
model_validate_json(): Parses and validates data from a JSON string or bytes object. [1]
model_validate_strings(): Parses and validates data from a dictionary with string keys and values in JSON mode.
Serialization:
dict() or model_dump(): Converts the model instance into a dictionary.
json() or model_dump_json(): Serializes the model instance into a JSON string.
Other Useful Methods:
copy(): Creates a deep copy of the model instance.
eq(): Checks if two model instances are equal.
repr(): Returns a string representation of the model instance.
fields: Returns a dictionary of the model’s fields.
Adding Custom Methods:
You can also add your own custom methods to a BaseModel class just like any other Python class. This allows you to add business logic, data manipulation, or any other functionality that you need.
from pydantic import BaseModel
class User(BaseModel):
name: str
age: int
def greet(self):
return f"Hello, {self.name}!"user = User(name="Alice", age=30)print(user.greet()) # Output: Hello, Alice!
Maybe you can go on GitHub and start asking the “why” of the SDK at all. It’s becoming very heavy, when all a language model developer really wants is to send their JSON to the RESTful AI API. (or an API/SDK built on sending a more efficient standard, e.g. Google).