@@ -2911,14 +2911,20 @@ def convert_json_schema(
29112911 root_json_schema_dict: dict[str, Any],
29122912 api_option: Literal['VERTEX_AI', 'GEMINI_API'],
29132913 raise_error_on_unsupported_field: bool,
2914+ visited_refs: Optional[set[str]] = None,
29142915 ) -> 'Schema':
2916+ if visited_refs is None:
2917+ visited_refs = set()
2918+
29152919 schema = Schema()
29162920 json_schema_dict = current_json_schema.model_dump()
29172921
2918- if json_schema_dict.get('ref'):
2919- json_schema_dict = _resolve_ref(
2920- json_schema_dict['ref'], root_json_schema_dict
2921- )
2922+ ref = json_schema_dict.get('ref')
2923+ if ref:
2924+ if ref in visited_refs:
2925+ return Schema()
2926+ visited_refs.add(ref)
2927+ json_schema_dict = _resolve_ref(ref, root_json_schema_dict)
29222928
29232929 raise_error_if_cannot_convert(
29242930 json_schema_dict=json_schema_dict,
@@ -2985,6 +2991,7 @@ def convert_json_schema(
29852991 root_json_schema_dict=root_json_schema_dict,
29862992 api_option=api_option,
29872993 raise_error_on_unsupported_field=raise_error_on_unsupported_field,
2994+ visited_refs=visited_refs,
29882995 )
29892996 setattr(schema, field_name, schema_field_value)
29902997 elif field_name in list_schema_field_names:
@@ -2994,6 +3001,7 @@ def convert_json_schema(
29943001 root_json_schema_dict=root_json_schema_dict,
29953002 api_option=api_option,
29963003 raise_error_on_unsupported_field=raise_error_on_unsupported_field,
3004+ visited_refs=visited_refs,
29973005 )
29983006 for this_field_value in field_value
29993007 ]
@@ -3007,6 +3015,7 @@ def convert_json_schema(
30073015 root_json_schema_dict=root_json_schema_dict,
30083016 api_option=api_option,
30093017 raise_error_on_unsupported_field=raise_error_on_unsupported_field,
3018+ visited_refs=visited_refs,
30103019 )
30113020 for key, value in field_value.items()
30123021 }
@@ -3051,6 +3060,8 @@ def convert_json_schema(
30513060 if default_value is not None:
30523061 schema.default = default_value
30533062
3063+ if ref:
3064+ visited_refs.remove(ref)
30543065 return schema
30553066
30563067 # This is the initial call to the recursive function.
0 commit comments