Skip to content

Format creation

Create a generic Response model using a dynamically defined Pydantic schema.

This function allows you to define a custom set of fields (e.g., for QnA-style data), and returns a typed Response[CustomQnA] model class.

Parameters:

Name Type Description Default
fields dict

A dictionary of field definitions for the inner model. Example: fields = { "question": (str, ...), "answer": (str, ...), "explanation": (str, ...), }

required

Returns:

Type Description
Type[Response]

Type[Response]: A Response model class parameterized with the dynamically created schema.

Source code in urbanworm/format_creation.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def create_format(fields: dict) -> Type[Response]:
    """
    Create a generic `Response` model using a dynamically defined Pydantic schema.

    This function allows you to define a custom set of fields (e.g., for QnA-style data),
    and returns a typed `Response[CustomQnA]` model class.

    Args:
        fields (dict): A dictionary of field definitions for the inner model.
            Example:
                fields = {
                    "question": (str, ...),
                    "answer": (str, ...),
                    "explanation": (str, ...),
                }

    Returns:
        Type[Response]: A `Response` model class parameterized with the dynamically created schema.
    """

    # Dynamically create the model
    CustomQnA = schema(fields)
    return Response[CustomQnA]