I am currently using tooldantic for structured outputs in batch mode in order to produce a json output structure.
I read the following page already but I am still acing issue.
“using-pydantic-structured-outputs-in-batch-mode”
Currently I am defining the following class
from tooldantic import OpenAiResponseFormatBaseModel as BaseModel
class Scores(BaseModel):
Score_1: int
Score_2: int
(Note that the name of scores here are just to explain what I want to achieve)
But my issue is that depending on what the user wants sometimes that class should contain 1 score or 2 or n…
My issue is that I do not want to have n class and just pick the correct one.
How could I generate that class dynamically?
I did try modelBuilder but at runtime I then have errors
For instance
{"id": "batch_req_6723a83b7da88190a978536913103710", "custom_id": "answer-0", "response": {"status_code": 400, "request_id": "....", "body": {"error": {"message": "Invalid value: 'object'. Supported values are: 'json_object', 'json_schema', and 'text'.", "type": "invalid_request_error", "param": "response_format.type", "code": "invalid_value"}}}, "error": null}
I also did try with ‘type’ there I also have an error
from typing import List, Optional, Dict, Type
def create_score_class(scores: Dict[str, Type]):
#Dynamically creates a class for sentence classification scores with the given score attributes.
attributes = {score: 0 for score in scores}
return type("SentenceClassificationScores", (BaseModel,), attributes)
myclass = create_score_class({"1":int, "2":int})
But I do have the following error:
pydantic.errors.PydanticUserError: A non-annotated attribute was detected: `1 = 0`. All model fields require a type annotation; if `1` is not meant to be a field, you may be able to resolve this error by annotating it as a `ClassVar` or updating `model_config['ignored_types']`.
So I do not succeed to have a valid dynamic json_schema.
Is there a way to do so without writing the complete json string?
Of course the prompt will also be generated accordingly.