Skip to content

Commit 624c8e7

Browse files
yyyu-googlecopybara-github
authored andcommitted
feat: enable json schema for controlled output and function declaration.
PiperOrigin-RevId: 771251820
1 parent f90af49 commit 624c8e7

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

google/genai/_live_converters.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,23 @@ def _FunctionDeclaration_to_mldev(
517517
if getv(from_object, ['parameters']) is not None:
518518
setv(to_object, ['parameters'], getv(from_object, ['parameters']))
519519

520+
if getv(from_object, ['parameters_json_schema']) is not None:
521+
setv(
522+
to_object,
523+
['parametersJsonSchema'],
524+
getv(from_object, ['parameters_json_schema']),
525+
)
526+
520527
if getv(from_object, ['response']) is not None:
521528
setv(to_object, ['response'], getv(from_object, ['response']))
522529

530+
if getv(from_object, ['response_json_schema']) is not None:
531+
setv(
532+
to_object,
533+
['responseJsonSchema'],
534+
getv(from_object, ['response_json_schema']),
535+
)
536+
523537
return to_object
524538

525539

@@ -541,9 +555,23 @@ def _FunctionDeclaration_to_vertex(
541555
if getv(from_object, ['parameters']) is not None:
542556
setv(to_object, ['parameters'], getv(from_object, ['parameters']))
543557

558+
if getv(from_object, ['parameters_json_schema']) is not None:
559+
setv(
560+
to_object,
561+
['parametersJsonSchema'],
562+
getv(from_object, ['parameters_json_schema']),
563+
)
564+
544565
if getv(from_object, ['response']) is not None:
545566
setv(to_object, ['response'], getv(from_object, ['response']))
546567

568+
if getv(from_object, ['response_json_schema']) is not None:
569+
setv(
570+
to_object,
571+
['responseJsonSchema'],
572+
getv(from_object, ['response_json_schema']),
573+
)
574+
547575
return to_object
548576

549577

google/genai/_tokens_converters.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,23 @@ def _FunctionDeclaration_to_mldev(
517517
if getv(from_object, ['parameters']) is not None:
518518
setv(to_object, ['parameters'], getv(from_object, ['parameters']))
519519

520+
if getv(from_object, ['parameters_json_schema']) is not None:
521+
setv(
522+
to_object,
523+
['parametersJsonSchema'],
524+
getv(from_object, ['parameters_json_schema']),
525+
)
526+
520527
if getv(from_object, ['response']) is not None:
521528
setv(to_object, ['response'], getv(from_object, ['response']))
522529

530+
if getv(from_object, ['response_json_schema']) is not None:
531+
setv(
532+
to_object,
533+
['responseJsonSchema'],
534+
getv(from_object, ['response_json_schema']),
535+
)
536+
523537
return to_object
524538

525539

@@ -541,9 +555,23 @@ def _FunctionDeclaration_to_vertex(
541555
if getv(from_object, ['parameters']) is not None:
542556
setv(to_object, ['parameters'], getv(from_object, ['parameters']))
543557

558+
if getv(from_object, ['parameters_json_schema']) is not None:
559+
setv(
560+
to_object,
561+
['parametersJsonSchema'],
562+
getv(from_object, ['parameters_json_schema']),
563+
)
564+
544565
if getv(from_object, ['response']) is not None:
545566
setv(to_object, ['response'], getv(from_object, ['response']))
546567

568+
if getv(from_object, ['response_json_schema']) is not None:
569+
setv(
570+
to_object,
571+
['responseJsonSchema'],
572+
getv(from_object, ['response_json_schema']),
573+
)
574+
547575
return to_object
548576

549577

google/genai/caches.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,23 @@ def _FunctionDeclaration_to_mldev(
194194
if getv(from_object, ['parameters']) is not None:
195195
setv(to_object, ['parameters'], getv(from_object, ['parameters']))
196196

197+
if getv(from_object, ['parameters_json_schema']) is not None:
198+
setv(
199+
to_object,
200+
['parametersJsonSchema'],
201+
getv(from_object, ['parameters_json_schema']),
202+
)
203+
197204
if getv(from_object, ['response']) is not None:
198205
setv(to_object, ['response'], getv(from_object, ['response']))
199206

207+
if getv(from_object, ['response_json_schema']) is not None:
208+
setv(
209+
to_object,
210+
['responseJsonSchema'],
211+
getv(from_object, ['response_json_schema']),
212+
)
213+
200214
return to_object
201215

202216

@@ -866,9 +880,23 @@ def _FunctionDeclaration_to_vertex(
866880
if getv(from_object, ['parameters']) is not None:
867881
setv(to_object, ['parameters'], getv(from_object, ['parameters']))
868882

883+
if getv(from_object, ['parameters_json_schema']) is not None:
884+
setv(
885+
to_object,
886+
['parametersJsonSchema'],
887+
getv(from_object, ['parameters_json_schema']),
888+
)
889+
869890
if getv(from_object, ['response']) is not None:
870891
setv(to_object, ['response'], getv(from_object, ['response']))
871892

893+
if getv(from_object, ['response_json_schema']) is not None:
894+
setv(
895+
to_object,
896+
['responseJsonSchema'],
897+
getv(from_object, ['response_json_schema']),
898+
)
899+
872900
return to_object
873901

874902

google/genai/models.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,23 @@ def _FunctionDeclaration_to_mldev(
319319
if getv(from_object, ['parameters']) is not None:
320320
setv(to_object, ['parameters'], getv(from_object, ['parameters']))
321321

322+
if getv(from_object, ['parameters_json_schema']) is not None:
323+
setv(
324+
to_object,
325+
['parametersJsonSchema'],
326+
getv(from_object, ['parameters_json_schema']),
327+
)
328+
322329
if getv(from_object, ['response']) is not None:
323330
setv(to_object, ['response'], getv(from_object, ['response']))
324331

332+
if getv(from_object, ['response_json_schema']) is not None:
333+
setv(
334+
to_object,
335+
['responseJsonSchema'],
336+
getv(from_object, ['response_json_schema']),
337+
)
338+
325339
return to_object
326340

327341

@@ -1787,9 +1801,23 @@ def _FunctionDeclaration_to_vertex(
17871801
if getv(from_object, ['parameters']) is not None:
17881802
setv(to_object, ['parameters'], getv(from_object, ['parameters']))
17891803

1804+
if getv(from_object, ['parameters_json_schema']) is not None:
1805+
setv(
1806+
to_object,
1807+
['parametersJsonSchema'],
1808+
getv(from_object, ['parameters_json_schema']),
1809+
)
1810+
17901811
if getv(from_object, ['response']) is not None:
17911812
setv(to_object, ['response'], getv(from_object, ['response']))
17921813

1814+
if getv(from_object, ['response_json_schema']) is not None:
1815+
setv(
1816+
to_object,
1817+
['responseJsonSchema'],
1818+
getv(from_object, ['response_json_schema']),
1819+
)
1820+
17931821
return to_object
17941822

17951823

google/genai/types.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,10 +1916,18 @@ class FunctionDeclaration(_common.BaseModel):
19161916
default=None,
19171917
description="""Optional. Describes the parameters to this function in JSON Schema Object format. Reflects the Open API 3.03 Parameter Object. string Key: the name of the parameter. Parameter names are case sensitive. Schema Value: the Schema defining the type used for the parameter. For function with no parameters, this can be left unset. Parameter names must start with a letter or an underscore and must only contain chars a-z, A-Z, 0-9, or underscores with a maximum length of 64. Example with 1 required and 1 optional parameter: type: OBJECT properties: param1: type: STRING param2: type: INTEGER required: - param1""",
19181918
)
1919+
parameters_json_schema: Optional[Any] = Field(
1920+
default=None,
1921+
description="""Optional. Describes the parameters to the function in JSON Schema format. The schema must describe an object where the properties are the parameters to the function. For example: ``` { "type": "object", "properties": { "name": { "type": "string" }, "age": { "type": "integer" } }, "additionalProperties": false, "required": ["name", "age"], "propertyOrdering": ["name", "age"] } ``` This field is mutually exclusive with `parameters`.""",
1922+
)
19191923
response: Optional[Schema] = Field(
19201924
default=None,
19211925
description="""Optional. Describes the output from this function in JSON Schema format. Reflects the Open API 3.03 Response Object. The Schema defines the type used for the response value of the function.""",
19221926
)
1927+
response_json_schema: Optional[Any] = Field(
1928+
default=None,
1929+
description="""Optional. Describes the output from this function in JSON Schema format. The value specified by the schema is the response value of the function. This field is mutually exclusive with `response`.""",
1930+
)
19231931

19241932
@classmethod
19251933
def from_callable_with_api_option(
@@ -2041,9 +2049,15 @@ class FunctionDeclarationDict(TypedDict, total=False):
20412049
parameters: Optional[SchemaDict]
20422050
"""Optional. Describes the parameters to this function in JSON Schema Object format. Reflects the Open API 3.03 Parameter Object. string Key: the name of the parameter. Parameter names are case sensitive. Schema Value: the Schema defining the type used for the parameter. For function with no parameters, this can be left unset. Parameter names must start with a letter or an underscore and must only contain chars a-z, A-Z, 0-9, or underscores with a maximum length of 64. Example with 1 required and 1 optional parameter: type: OBJECT properties: param1: type: STRING param2: type: INTEGER required: - param1"""
20432051

2052+
parameters_json_schema: Optional[Any]
2053+
"""Optional. Describes the parameters to the function in JSON Schema format. The schema must describe an object where the properties are the parameters to the function. For example: ``` { "type": "object", "properties": { "name": { "type": "string" }, "age": { "type": "integer" } }, "additionalProperties": false, "required": ["name", "age"], "propertyOrdering": ["name", "age"] } ``` This field is mutually exclusive with `parameters`."""
2054+
20442055
response: Optional[SchemaDict]
20452056
"""Optional. Describes the output from this function in JSON Schema format. Reflects the Open API 3.03 Response Object. The Schema defines the type used for the response value of the function."""
20462057

2058+
response_json_schema: Optional[Any]
2059+
"""Optional. Describes the output from this function in JSON Schema format. The value specified by the schema is the response value of the function. This field is mutually exclusive with `response`."""
2060+
20472061

20482062
FunctionDeclarationOrDict = Union[FunctionDeclaration, FunctionDeclarationDict]
20492063

@@ -6705,6 +6719,10 @@ class GenerationConfig(_common.BaseModel):
67056719
default=None,
67066720
description="""Optional. The `Schema` object allows the definition of input and output data types. These types can be objects, but also primitives and arrays. Represents a select subset of an [OpenAPI 3.0 schema object](https://spec.openapis.org/oas/v3.0.3#schema). If set, a compatible response_mime_type must also be set. Compatible mimetypes: `application/json`: Schema for JSON response.""",
67076721
)
6722+
response_json_schema: Optional[Any] = Field(
6723+
default=None,
6724+
description="""Optional. Output schema of the generated response. This is an alternative to `response_schema` that accepts [JSON Schema](https://json-schema.org/). If set, `response_schema` must be omitted, but `response_mime_type` is required. While the full JSON Schema may be sent, not all features are supported. Specifically, only the following properties are supported: - `$id` - `$defs` - `$ref` - `$anchor` - `type` - `format` - `title` - `description` - `enum` (for strings and numbers) - `items` - `prefixItems` - `minItems` - `maxItems` - `minimum` - `maximum` - `anyOf` - `oneOf` (interpreted the same as `anyOf`) - `properties` - `additionalProperties` - `required` The non-standard `propertyOrdering` property may also be set. Cyclic references are unrolled to a limited degree and, as such, may only be used within non-required properties. (Nullable properties are not sufficient.) If `$ref` is set on a sub-schema, no other properties, except for than those starting as a `$`, may be set.""",
6725+
)
67086726
routing_config: Optional[GenerationConfigRoutingConfig] = Field(
67096727
default=None, description="""Optional. Routing configuration."""
67106728
)
@@ -6772,6 +6790,9 @@ class GenerationConfigDict(TypedDict, total=False):
67726790
response_schema: Optional[SchemaDict]
67736791
"""Optional. The `Schema` object allows the definition of input and output data types. These types can be objects, but also primitives and arrays. Represents a select subset of an [OpenAPI 3.0 schema object](https://spec.openapis.org/oas/v3.0.3#schema). If set, a compatible response_mime_type must also be set. Compatible mimetypes: `application/json`: Schema for JSON response."""
67746792

6793+
response_json_schema: Optional[Any]
6794+
"""Optional. Output schema of the generated response. This is an alternative to `response_schema` that accepts [JSON Schema](https://json-schema.org/). If set, `response_schema` must be omitted, but `response_mime_type` is required. While the full JSON Schema may be sent, not all features are supported. Specifically, only the following properties are supported: - `$id` - `$defs` - `$ref` - `$anchor` - `type` - `format` - `title` - `description` - `enum` (for strings and numbers) - `items` - `prefixItems` - `minItems` - `maxItems` - `minimum` - `maximum` - `anyOf` - `oneOf` (interpreted the same as `anyOf`) - `properties` - `additionalProperties` - `required` The non-standard `propertyOrdering` property may also be set. Cyclic references are unrolled to a limited degree and, as such, may only be used within non-required properties. (Nullable properties are not sufficient.) If `$ref` is set on a sub-schema, no other properties, except for than those starting as a `$`, may be set."""
6795+
67756796
routing_config: Optional[GenerationConfigRoutingConfigDict]
67766797
"""Optional. Routing configuration."""
67776798

0 commit comments

Comments
 (0)