diff --git a/jambo/parser/object_type_parser.py b/jambo/parser/object_type_parser.py index d3c27ca..92b6a9f 100644 --- a/jambo/parser/object_type_parser.py +++ b/jambo/parser/object_type_parser.py @@ -46,9 +46,10 @@ class ObjectTypeParser(GenericTypeParser): type_parsing.model_validate(example) for example in example_values ] - if name in ref_cache: + if name in ref_cache and isinstance(ref_cache[name], type): warnings.warn( - f"Type '{name}' is already in the ref_cache and will be overwritten.", + f"Type '{name}' is already in the ref_cache and will be overwritten." + " This may indicate a circular reference in the schema or a collision in the schema.", UserWarning, ) ref_cache[name] = type_parsing diff --git a/jambo/schema_converter.py b/jambo/schema_converter.py index 6358786..1ba52d6 100644 --- a/jambo/schema_converter.py +++ b/jambo/schema_converter.py @@ -26,14 +26,28 @@ class SchemaConverter: self, schema: JSONSchema, ref_cache: Optional[RefCacheDict] = None, + without_cache: bool = False, ) -> type[BaseModel]: """ Converts a JSON Schema to a Pydantic model. + This is the instance method version of `build` and uses the instance's reference cache if none is provided. + Use this method if you want to utilize the instance's reference cache. + :param schema: The JSON Schema to convert. :param ref_cache: An optional reference cache to use during conversion. + :param without_cache: Whether to use a clean reference cache for this conversion. :return: The generated Pydantic model. """ - return self.build(schema, ref_cache or self._ref_cache) + local_ref_cache: RefCacheDict + + if without_cache: + local_ref_cache = dict() + elif ref_cache is None: + local_ref_cache = self._ref_cache + else: + local_ref_cache = ref_cache + + return self.build(schema, local_ref_cache) @staticmethod def build( @@ -43,7 +57,6 @@ class SchemaConverter: Converts a JSON Schema to a Pydantic model. :param schema: The JSON Schema to convert. :param ref_cache: An optional reference cache to use during conversion, if provided `with_clean_cache` will be ignored. - :param with_clean_cache: Whether to use a clean reference cache for this conversion. Set to rue due to API compatibility. Will be set to False in future versions. :return: The generated Pydantic model. """ if ref_cache is None: