Skip to content

Commit f30c36d

Browse files
Mattias WighMattias Wigh
Mattias Wigh
authored and
Mattias Wigh
committed
Needs to set required fields as required
1 parent 8f77cc6 commit f30c36d

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

google/generativeai/types/content_types.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import mimetypes
2222
import pathlib
2323
import typing
24-
from typing import Any, Callable, Union
25-
from typing_extensions import TypedDict
24+
from typing import Any, Callable, Union, get_type_hints, get_origin, get_args
25+
from typing_extensions import TypedDict, is_typeddict
2626

2727
import pydantic
2828

@@ -336,7 +336,18 @@ def to_contents(contents: ContentsType) -> list[protos.Content]:
336336

337337
def _schema_for_class(cls: TypedDict) -> dict[str, Any]:
338338
schema = _build_schema("dummy", {"dummy": (cls, pydantic.Field())})
339-
return schema["properties"]["dummy"]
339+
properties = schema["properties"]["dummy"]
340+
if is_typeddict(cls):
341+
required_keys = []
342+
type_hints = get_type_hints(cls)
343+
for key, type_hint in type_hints.items():
344+
if key in cls.__required_keys__:
345+
# Check if the type is Optional, i.e., Union[..., NoneType]
346+
if get_origin(type_hint) is Union and type(None) in get_args(type_hint):
347+
continue
348+
required_keys.append(key)
349+
properties["required"] = required_keys
350+
return properties
340351

341352

342353
def _schema_for_function(

0 commit comments

Comments
 (0)