JSON Schema generation
conatus.actions.json_schema
¶
Types and methods to generate the JSON Schema of Actions.
Generating a JSONSchema for an Action is
important since LLM APIs tend to requite a JSON Schema for the input parameters
of the function.
The main method here is generate_openai_json_schema
, which generates
the JSON Schema for an Action. We do this
by first generating Pydantic models from the Action
's FunctionInfo
, one of which will be
a Pydantic model specifically for the JSON Schema. It is that model that is
the input to generate_openai_json_schema
.
In practice, it will be used like this:
from conatus.actions.utils.schema_extraction import (
extract_function_info,
generate_pydantic_models,
)
from conatus.actions.json_schema import generate_openai_json_schema
def func(a: int, b: str) -> str:
...
function_info = extract_function_info(func)
pydantic_models = generate_pydantic_models(function_info)
json_schema = generate_openai_json_schema(pydantic_models.json_schema)
Strict vs lenient mode¶
We can generate the JSON Schema in "lenient" mode or "strict" mode. The "strict" mode is more restrictive, is tailor made for OpenAI's Structured Outputs feature, and guarantees us that what will be returned by the LLM corresponds exactly to the specifications of the action.
Reference¶
Note that OpenAI tend to deprecate their documentation quite aggressively. Sorry if the links are broken.
PropertyConstPydanticJSON
module-attribute
¶
PropertyConstPydanticJSON = (
Annotated[StringPydanticJSON, Tag("string")]
| Annotated[ObjectPydanticJSON, Tag("object")]
| Annotated[ArrayPydanticJSON, Tag("array")]
| Annotated[NumberPydanticJSON, Tag("number")]
| Annotated[NumberPydanticJSON, Tag("integer")]
| Annotated[BooleanPydanticJSON, Tag("boolean")]
| Annotated[NullPydanticJSON, Tag("null")]
| Annotated[AnyPydanticJSON, Tag("any")]
)
Pydantic JSON schema for a constant value.
PropertyPydanticJSON
module-attribute
¶
PropertyPydanticJSON = Annotated[
Annotated[AnyOfPydanticJSON, Tag("anyOf")]
| PropertyConstPydanticJSON
| Annotated[RefPydanticJSON, Tag("ref")],
Discriminator(get_pyd_json_schema_type),
]
Pydantic JSON schema for a property.
PropertyConstOpenAIStrictJSON
module-attribute
¶
PropertyConstOpenAIStrictJSON = (
Annotated[StringOpenAIStrictJSON, Tag("string")]
| Annotated[ObjectOpenAIStrictJSON, Tag("object")]
| Annotated[ArrayOpenAIStrictJSON, Tag("array")]
| Annotated[NumberOpenAIStrictJSON, Tag("number")]
| Annotated[NumberOpenAIStrictJSON, Tag("integer")]
| Annotated[BooleanOpenAIStrictJSON, Tag("boolean")]
| Annotated[NullOpenAIStrictJSON, Tag("null")]
| Annotated[AnyPydanticJSON, Tag("any")]
)
OpenAI JSON schema for a constant value (strict mode).
PropertyOpenAIStrictJSON
module-attribute
¶
PropertyOpenAIStrictJSON = Annotated[
Annotated[AnyOfOpenAIStrictJSON, Tag("anyOf")]
| PropertyConstOpenAIStrictJSON
| Annotated[RefOpenAIStrictJSON, Tag("ref")],
Discriminator(get_pyd_json_schema_type),
]
OpenAI JSON schema for a property (strict mode).
AnyPydanticJSON
¶
Bases: NoExtraBaseModel
Base model for a JSON schema representing any type.
| ATTRIBUTE | DESCRIPTION |
|---|---|
title |
The title of the schema.
TYPE:
|
description |
The description of the schema.
TYPE:
|
remove_refs
¶
remove_refs(defs: dict[str, PropertyPydanticJSON]) -> Self
Remove references from the JSON schema.
| PARAMETER | DESCRIPTION |
|---|---|
defs
|
The definitions of the schema.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Self
|
The JSON schema without references. |
Source code in conatus/actions/json_schema.py
StringPydanticJSON
¶
Bases: NoExtraBaseModel
Pydantic JSON schema for a string.
| ATTRIBUTE | DESCRIPTION |
|---|---|
title |
The title of the schema.
TYPE:
|
description |
The description of the schema.
TYPE:
|
enum |
The possible values of the string. |
default |
The default value of the string.
TYPE:
|
max_length |
The maximum length of the string.
TYPE:
|
min_length |
The minimum length of the string.
TYPE:
|
pattern |
The pattern of the string.
TYPE:
|
format |
The format of the string.
TYPE:
|
type_ |
The type of the string.
TYPE:
|
remove_refs
¶
remove_refs(defs: dict[str, PropertyPydanticJSON]) -> Self
Remove references from the JSON schema.
| PARAMETER | DESCRIPTION |
|---|---|
defs
|
The definitions of the schema.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Self
|
The JSON schema without references. |
Source code in conatus/actions/json_schema.py
NumberPydanticJSON
¶
Bases: NoExtraBaseModel
Pydantic JSON schema for a number (int or float).
| ATTRIBUTE | DESCRIPTION |
|---|---|
title |
The title of the schema.
TYPE:
|
description |
The description of the schema.
TYPE:
|
enum |
The possible values of the number. |
default |
The default value of the number. |
type_ |
The type of the number.
TYPE:
|
exclusive_maximum |
The exclusive maximum value of the number.
TYPE:
|
exclusive_minimum |
The exclusive minimum value of the number.
TYPE:
|
maximum |
The maximum value of the number. |
minimum |
The minimum value of the number. |
remove_refs
¶
remove_refs(defs: dict[str, PropertyPydanticJSON]) -> Self
Remove references from the JSON schema.
| PARAMETER | DESCRIPTION |
|---|---|
defs
|
The definitions of the schema.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Self
|
The JSON schema without references. |
Source code in conatus/actions/json_schema.py
BooleanPydanticJSON
¶
Bases: NoExtraBaseModel
Pydantic JSON schema for a boolean.
| ATTRIBUTE | DESCRIPTION |
|---|---|
description |
The description of the schema.
TYPE:
|
default |
The default value of the boolean.
TYPE:
|
title |
The title of the schema.
TYPE:
|
remove_refs
¶
remove_refs(defs: dict[str, PropertyPydanticJSON]) -> Self
Remove references from the JSON schema.
| PARAMETER | DESCRIPTION |
|---|---|
defs
|
The definitions of the schema.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Self
|
The JSON schema without references. |
Source code in conatus/actions/json_schema.py
ObjectPydanticJSON
¶
Bases: NoExtraBaseModel
Pydantic JSON schema for an object.
| ATTRIBUTE | DESCRIPTION |
|---|---|
description |
The description of the schema.
TYPE:
|
properties |
The properties of the object.
TYPE:
|
additional_properties |
Whether additional properties are allowed.
TYPE:
|
default |
The default value of the object.
TYPE:
|
required |
The required properties of the object. |
title |
The title of the schema.
TYPE:
|
remove_refs
¶
remove_refs(defs: dict[str, PropertyPydanticJSON]) -> Self
Remove references from the JSON schema.
| PARAMETER | DESCRIPTION |
|---|---|
defs
|
The definitions of the schema.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Self
|
The JSON schema without references. |
Source code in conatus/actions/json_schema.py
ArrayPydanticJSON
¶
Bases: NoExtraBaseModel
Pydantic JSON schema for an array.
| ATTRIBUTE | DESCRIPTION |
|---|---|
title |
The title of the schema.
TYPE:
|
description |
The description of the schema.
TYPE:
|
items |
The items of the array.
TYPE:
|
max_items |
The maximum number of items in the array.
TYPE:
|
min_items |
The minimum number of items in the array.
TYPE:
|
prefix_items |
The prefix items of the array.
TYPE:
|
default |
The default value of the array.
TYPE:
|
unique_items |
Whether the items in the array are unique.
TYPE:
|
type_ |
The type of the array.
TYPE:
|
remove_refs
¶
remove_refs(defs: dict[str, PropertyPydanticJSON]) -> Self
Remove references from the JSON schema.
| PARAMETER | DESCRIPTION |
|---|---|
defs
|
The definitions of the schema.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Self
|
The JSON schema without references. |
Source code in conatus/actions/json_schema.py
NullPydanticJSON
¶
Bases: NoExtraBaseModel
Pydantic JSON schema for a null value.
| ATTRIBUTE | DESCRIPTION |
|---|---|
title |
The title of the schema.
TYPE:
|
description |
The description of the schema.
TYPE:
|
type_ |
The type of the schema.
TYPE:
|
default |
The default value of the schema.
TYPE:
|
remove_refs
¶
remove_refs(defs: dict[str, PropertyPydanticJSON]) -> Self
Remove references from the JSON schema.
| PARAMETER | DESCRIPTION |
|---|---|
defs
|
The definitions of the schema.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Self
|
The JSON schema without references. |
Source code in conatus/actions/json_schema.py
RefPydanticJSON
¶
Bases: NoExtraBaseModel
Pydantic JSON schema for a reference.
| ATTRIBUTE | DESCRIPTION |
|---|---|
title |
The title of the schema.
TYPE:
|
description |
The description of the schema.
TYPE:
|
ref |
The reference of the schema.
TYPE:
|
default |
The default value of the schema.
TYPE:
|
remove_refs
¶
remove_refs(
defs: dict[str, PropertyPydanticJSON],
) -> Self | PropertyPydanticJSON
Remove references from the JSON schema.
| PARAMETER | DESCRIPTION |
|---|---|
defs
|
The definitions of the schema.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Self | PropertyPydanticJSON
|
The JSON schema without references. |
| RAISES | DESCRIPTION |
|---|---|
JSONSchemaIncorrectRefsError
|
If the reference is not found in the definitions. |
Source code in conatus/actions/json_schema.py
AnyOfPydanticJSON
¶
Bases: NoExtraBaseModel
Pydantic JSON schema for a union of types.
| ATTRIBUTE | DESCRIPTION |
|---|---|
title |
The title of the schema.
TYPE:
|
description |
The description of the schema.
TYPE:
|
any_of |
The union of types.
TYPE:
|
default |
The default value of the schema.
TYPE:
|
remove_refs
¶
remove_refs(defs: dict[str, PropertyPydanticJSON]) -> Self
Remove references from the JSON schema.
| PARAMETER | DESCRIPTION |
|---|---|
defs
|
The definitions of the schema.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Self
|
The JSON schema without references. |
Source code in conatus/actions/json_schema.py
PydanticJSONSchema
¶
Bases: NoExtraBaseModel
Pydantic JSON schema.
This should be what Pydantic returns with model_json_schema().
| ATTRIBUTE | DESCRIPTION |
|---|---|
properties |
The properties of the schema.
TYPE:
|
required |
The required properties of the schema. |
title |
The title of the schema.
TYPE:
|
description |
The description of the schema.
TYPE:
|
type_ |
The type of the schema.
TYPE:
|
defs |
The definitions of the schema.
TYPE:
|
remove_refs
¶
Remove references from the JSON schema.
Note this is happening in-place, so the original JSON schema is modified.
Source code in conatus/actions/json_schema.py
OpenAIJSONSchemaParameters
¶
Bases: IgnoreExtraBaseModel
OpenAI JSON schema for function parameters (lenient mode).
| ATTRIBUTE | DESCRIPTION |
|---|---|
properties |
The properties of the schema.
TYPE:
|
required |
The required properties of the schema. |
type_ |
The type of the schema.
TYPE:
|
defs |
The definitions of the schema.
TYPE:
|
OpenAIJSONSchema
¶
Bases: NoExtraBaseModel
OpenAI JSON schema for functions (lenient mode).
This represents an individual function.
| ATTRIBUTE | DESCRIPTION |
|---|---|
name |
The name of the function.
TYPE:
|
description |
The description of the function.
TYPE:
|
parameters |
The parameters of the function. |
strict |
Whether the function is strict.
TYPE:
|
ArrayOpenAIStrictJSON
¶
Bases: NoExtraBaseModel
OpenAI JSON schema for an array (strict mode).
| ATTRIBUTE | DESCRIPTION |
|---|---|
title |
The title of the schema.
TYPE:
|
description |
The description of the schema.
TYPE:
|
items |
The items of the array.
TYPE:
|
type_ |
The type of the array.
TYPE:
|
StringOpenAIStrictJSON
¶
Bases: NoExtraBaseModel
OpenAI JSON schema for a string (strict mode).
| ATTRIBUTE | DESCRIPTION |
|---|---|
title |
The title of the schema.
TYPE:
|
description |
The description of the schema.
TYPE:
|
enum |
The possible values of the string. |
type_ |
The type of the string.
TYPE:
|
ObjectOpenAIStrictJSON
¶
Bases: NoExtraBaseModel
OpenAI JSON schema for an object (strict mode).
| ATTRIBUTE | DESCRIPTION |
|---|---|
title |
The title of the schema.
TYPE:
|
description |
The description of the schema.
TYPE:
|
properties |
The properties of the object.
TYPE:
|
additional_properties |
Whether additional properties are allowed.
TYPE:
|
required |
The required properties of the object. |
NumberOpenAIStrictJSON
¶
Bases: NoExtraBaseModel
OpenAI JSON schema for a number (strict mode).
| ATTRIBUTE | DESCRIPTION |
|---|---|
title |
The title of the schema.
TYPE:
|
description |
The description of the schema.
TYPE:
|
enum |
The possible values of the number. |
type_ |
The type of the number.
TYPE:
|
RefOpenAIStrictJSON
¶
Bases: NoExtraBaseModel
OpenAI JSON schema for a reference (strict mode).
| ATTRIBUTE | DESCRIPTION |
|---|---|
ref |
The reference of the schema.
TYPE:
|
AnyOfOpenAIStrictJSON
¶
Bases: NoExtraBaseModel
OpenAI JSON schema for a union of types (strict mode).
| ATTRIBUTE | DESCRIPTION |
|---|---|
title |
The title of the schema.
TYPE:
|
description |
The description of the schema.
TYPE:
|
any_of |
The union of types.
TYPE:
|
OpenAIJSONStrictSchemaParameters
¶
Bases: IgnoreExtraBaseModel
OpenAI JSON schema for function parameters (strict mode).
| ATTRIBUTE | DESCRIPTION |
|---|---|
properties |
The properties of the schema.
TYPE:
|
required |
The required properties of the schema. |
type_ |
The type of the schema.
TYPE:
|
defs |
The definitions of the schema.
TYPE:
|
OpenAIJSONStrictFnSchema
¶
Bases: NoExtraBaseModel
OpenAI JSON schema for functions (strict mode).
| ATTRIBUTE | DESCRIPTION |
|---|---|
name |
The name of the function.
TYPE:
|
description |
The description of the function.
TYPE:
|
parameters |
The parameters of the function. |
strict |
Whether the function is strict.
TYPE:
|
get_pyd_json_schema_type
¶
Get the type of a PydJSONSchema object.
JSONSchema objects generally have a 'type' element associated with
them, but if they don't, it's generally a sign that they are a union
(anyOf) or a reference ($ref). This function helps Pydantic
determine which type of JSONSchema object it is.
For more information, see the Pydantic documentation on unions and discriminators
| PARAMETER | DESCRIPTION |
|---|---|
v
|
The JSONSchema object. |
| RETURNS | DESCRIPTION |
|---|---|
str
|
The tag of the Pydantic JSONSchema object.
TYPE:
|
Source code in conatus/actions/json_schema.py
cast_strict_prop
¶
cast_strict_prop(
jsc: AnyOfPydanticJSON, key_reference: list[str]
) -> tuple[AnyOfOpenAIStrictJSON, list[str]]
cast_strict_prop(
jsc: AnyPydanticJSON, key_reference: list[str]
) -> tuple[AnyPydanticJSON, list[str]]
cast_strict_prop(
jsc: RefPydanticJSON, key_reference: list[str]
) -> tuple[RefOpenAIStrictJSON, list[str]]
cast_strict_prop(
jsc: StringPydanticJSON, key_reference: list[str]
) -> tuple[StringOpenAIStrictJSON, list[str]]
cast_strict_prop(
jsc: ObjectPydanticJSON, key_reference: list[str]
) -> tuple[ObjectOpenAIStrictJSON, list[str]]
cast_strict_prop(
jsc: ArrayPydanticJSON, key_reference: list[str]
) -> tuple[ArrayOpenAIStrictJSON, list[str]]
cast_strict_prop(
jsc: NumberPydanticJSON, key_reference: list[str]
) -> tuple[NumberOpenAIStrictJSON, list[str]]
cast_strict_prop(
jsc: PropertyPydanticJSON, key_reference: list[str]
) -> tuple[PropertyOpenAIStrictJSON, list[str]]
Cast a Pydantic JSON schema to a strict OpenAI-compatible one.
| PARAMETER | DESCRIPTION |
|---|---|
jsc
|
The Pydantic JSON schema.
TYPE:
|
key_reference
|
The key reference. |
| RETURNS | DESCRIPTION |
|---|---|
tuple[PropertyOpenAIStrictJSON, list[str]]
|
The OpenAI-compatible JSON schema and any errors. |
Source code in conatus/actions/json_schema.py
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 | |
cast_strict_mode
¶
cast_strict_mode(
oai_jsc: OpenAIJSONSchema,
) -> OpenAIJSONStrictFnSchema
Cast the OpenAI JSON schema to strict mode.
| PARAMETER | DESCRIPTION |
|---|---|
oai_jsc
|
The OpenAI JSON schema.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
OpenAIJSONStrictFnSchema
|
The OpenAI JSON schema in strict mode. |
| RAISES | DESCRIPTION |
|---|---|
OpenAIStrictCastError
|
If the schema is incorrect. |
Source code in conatus/actions/json_schema.py
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 | |
generate_openai_json_schema
¶
generate_openai_json_schema(
json_schema_pydantic_model: (
type[BaseModel] | TypeAdapter[ParamType]
),
*,
strict_mode: bool = False,
convert_non_objects_to_objects: Literal[False] = False,
remove_refs: bool = False
) -> FunctionDefinition
generate_openai_json_schema(
json_schema_pydantic_model: (
type[BaseModel] | TypeAdapter[ParamType]
),
*,
strict_mode: bool = False,
convert_non_objects_to_objects: Literal[True],
remove_refs: bool = False
) -> tuple[FunctionDefinition, bool]
generate_openai_json_schema(
json_schema_pydantic_model: (
type[BaseModel] | TypeAdapter[ParamType]
),
*,
strict_mode: bool = False,
convert_non_objects_to_objects: bool = False,
remove_refs: bool = False
) -> FunctionDefinition | tuple[FunctionDefinition, bool]
Transform the Pydantic JSON Schema into an OpenAI-compatible one.
| PARAMETER | DESCRIPTION |
|---|---|
json_schema_pydantic_model
|
The Pydantic model (or TypeAdapter) representing the JSON schema of the action.
TYPE:
|
strict_mode
|
Whether to generate the schema in strict mode.
TYPE:
|
convert_non_objects_to_objects
|
Flag to allow the conversion of non-
object JSON schemas to object JSON schemas. This is only relevant
for
TYPE:
|
remove_refs
|
Whether to remove references from the JSON schema.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
FunctionDefinition | tuple[FunctionDefinition, bool]
|
Source code in conatus/actions/json_schema.py
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 | |
get_complete_json_schema
¶
get_complete_json_schema(
json_schema_pydantic_model: (
type[BaseModel] | TypeAdapter[ParamType]
),
*,
convert_non_objects_to_objects: bool = False
) -> tuple[dict[str, Any], bool]
Get the complete JSON schema for a Pydantic model.
| PARAMETER | DESCRIPTION |
|---|---|
json_schema_pydantic_model
|
The Pydantic model (or TypeAdapter) representing the JSON schema of the action.
TYPE:
|
convert_non_objects_to_objects
|
Flag to allow the conversion of non-
object JSON schemas to object JSON schemas. This is only relevant
for
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
The complete JSON schema. |
bool
|
Whether the schema was converted to an item object. |
Source code in conatus/actions/json_schema.py
generate_structured_output_openai_json_schema
¶
generate_structured_output_openai_json_schema(
json_schema_pydantic_model: (
type[BaseModel] | TypeAdapter[ParamType]
),
*,
strict_mode: bool = False
) -> tuple[StructuredOutputDefinition, bool]
Transform the Pydantic JSON Schema into an OpenAI-compatible one.
| PARAMETER | DESCRIPTION |
|---|---|
json_schema_pydantic_model
|
The Pydantic model representing the JSON schema of the action.
TYPE:
|
strict_mode
|
Whether to generate the schema in strict mode.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
StructuredOutputDefinition
|
The OpenAI-compatible JSON schema. |
bool
|
Whether the schema was converted to an item object. |