I'm writing some code to infer a json schema def inferschema(data): if isinstance(data,…
- User
I'm writing some code to infer a json schema
def infer_schema(data): if isinstance(data, dict): schema = {} for key, value in data.items(): schema[key] = infer_schema(value) return schema elif isinstance(data, list): if data: # check if the list is not empty child_schemas = [infer_schema(x) for x in data] union = {} for child_schemas in child_schemas: union.update(child_schemas) return [union] else: return [] else: return type(data).name
Problem is when i see the list: the update is not recursive i.e. If i see {'a': {'b': 3}}, and {'a': {'b': 3, 'c': 4}}, I want to update to the schema {'a': {'b': 'int', 'c': 'int'}}
Assistant
You're right, the current implementation doesn't handle nested structures within lists correctly. Let's modify the code to handle this case recursively. Here's an updated version of the
infer_schemafunction that should work for nested structures:python
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.