def gen_datamodel_code(
    source_file, format="json", jmespath=None, model_name=None
) -> str:
    """Generates Python code with Pydantic models that corresponds
    to the provided JSON, CSV, or JSONL file.
    It support root JSON arrays (samples the first entry).
    """
    data_string = ""
    # using uiid to get around issue #1617
    if not model_name:
        # comply with Python class names
        uid_str = str(generate_uuid()).replace("-", "")
        model_name = f"Model{format}{uid_str}"

    with source_file.open() as fd:  # CSV can be larger than memory
        if format == "csv":
            data_string += fd.readline().replace("\r", "")
            data_string += fd.readline().replace("\r", "")
        elif format == "jsonl":
            data_string = fd.readline().replace("\r", "")
        else:
            data_string = fd.read()  # other meta must fit into RAM

    if format in ("json", "jsonl"):
        json_object = process_json(data_string, jmespath)
        if format == "json" and isinstance(json_object, list):
            json_object = json_object[0]  # sample the 1st object from JSON array
        if format == "jsonl":
            format = "json"  # treat json line as plain JSON in auto-schema
        data_string = json.dumps(json_object)

    import datamodel_code_generator

    input_file_types = {i.value: i for i in datamodel_code_generator.InputFileType}
    input_file_type = input_file_types[format]
    with tempfile.TemporaryDirectory() as tmpdir:
        output = Path(tmpdir) / "model.py"
        datamodel_code_generator.generate(
            data_string,
            input_file_type=input_file_type,
            output=output,
            target_python_version=datamodel_code_generator.PythonVersion.PY_39,
            base_class="datachain.lib.meta_formats.UserModel",
            class_name=model_name,
            additional_imports=["datachain.lib.data_model.DataModel"],
            use_standard_collections=True,
        )
        epilogue = f"""
DataModel.register({model_name})
spec = {model_name}
"""
        return output.read_text() + epilogue
Generates Python code with Pydantic models that corresponds
    to the provided JSON, CSV, or JSONL file.
    It support root JSON arrays (samples the first entry).
    
src/datachain/lib/meta_formats.py