Skip to content

Provide type conversion among json schema and multiple-choice output types #1722

@RobinPicard

Description

@RobinPicard

Some models only support some types within the json schema and multiple-choice output types (for instance pydantic, but not json schema string for Gemini). It means that we need to handle all different types in the type adapter and that users need to know model-specific things.

Ideally, we would have be able to cast all types into each other, but if we can only do some that would already be good!

Examples of possible interfaces:

  1. Gemini model

The model only accepts dataclasses, types dicts and pydantic models. A JsonSchema or Genson schema builder will lead to an error.

elif is_dataclass(output_type):
    return self.format_json_output_type(output_type)
elif is_typed_dict(output_type):
    return self.format_json_output_type(output_type)
elif is_pydantic_model(output_type):
    return self.format_json_output_type(output_type)
# else error

We would have instead:

if json_schema(output_type): # Evaluates to yes for all json schema output types
  return JsonSchema.convert_to(output_type, ["dataclass", "typed_dict", "pydantic"])
  1. Ollama

The model needs the output type to be a json schema string. This is already implemented, but it's a bit wordy and we need to repeat it in all models with the same requirements.

elif isinstance(output_type, JsonSchema):
    return json.loads(output_type.schema)
elif is_dataclass(output_type):
    schema = TypeAdapter(output_type).json_schema()
    return schema
elif is_typed_dict(output_type):
    schema = TypeAdapter(output_type).json_schema()
    return schema
elif is_pydantic_model(output_type):
    schema = output_type.model_json_schema()
    return schema
elif is_genson_schema_builder(output_type):
    return output_type.to_json()

We would have instead:

if json_schema(output_type):
  return JsonSchema.convert_to(output_type, "schema_string")

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions