mlflow.types
The mlflow.types
module defines data types and utilities to be used by other mlflow
components to describe interface independent of other frameworks or languages.
-
class
mlflow.types.
ColSpec
(type: Union[mlflow.types.schema.Array, mlflow.types.schema.DataType, mlflow.types.schema.Map, mlflow.types.schema.Object, mlflow.types.schema.AnyType, str], name: Optional[str] = None, required: bool = True)[source] Bases:
object
Specification of name and type of a single column in a dataset.
-
classmethod
from_json_dict
(**kwargs)[source] Deserialize from a json loaded dictionary. The dictionary is expected to contain type and optional name and required keys.
-
classmethod
-
class
mlflow.types.
DataType
(value)[source] Bases:
enum.Enum
MLflow data types.
-
to_numpy
() → numpy.dtype[source] Get equivalent numpy data type.
-
to_pandas
() → numpy.dtype[source] Get equivalent pandas data type.
-
to_python
()[source] Get equivalent python data type.
-
-
class
mlflow.types.
ParamSchema
(params: list)[source] Bases:
object
Note
Experimental: This class may change or be removed in a future release without warning.
Specification of parameters applicable to the model. ParamSchema is represented as a list of
ParamSpec
.-
classmethod
from_json
(json_str: str)[source] Deserialize from a json string.
-
to_dict
() → list[source] Serialize into a jsonable dictionary.
-
to_json
() → str[source] Serialize into json string.
-
classmethod
-
class
mlflow.types.
ParamSpec
(name: str, dtype: Union[mlflow.types.schema.DataType, str], default: Optional[Union[mlflow.types.schema.DataType, list]], shape: Optional[tuple] = None)[source] Bases:
object
Note
Experimental: This class may change or be removed in a future release without warning.
Specification used to represent parameters for the model.
-
class
ParamSpecTypedDict
[source] Bases:
TypedDict
-
classmethod
enforce_param_datatype
(name, value, dtype: mlflow.types.schema.DataType)[source] Enforce the value matches the data type.
The following type conversions are allowed:
int -> long, float, double
long -> float, double
float -> double
any -> datetime (try conversion)
Any other type mismatch will raise error.
- Parameters
name – parameter name
value – parameter value
dtype – expected data type
-
classmethod
from_json_dict
(**kwargs)[source] Deserialize from a json loaded dictionary. The dictionary is expected to contain name, type and default keys.
-
classmethod
validate_type_and_shape
(spec: str, value: Optional[Union[mlflow.types.schema.DataType, list]], value_type: mlflow.types.schema.DataType, shape: Optional[tuple])[source] Validate that the value has the expected type and shape.
-
class
-
class
mlflow.types.
Schema
(inputs: list)[source] Bases:
object
Specification of a dataset.
Schema is represented as a list of
ColSpec
orTensorSpec
. A combination of ColSpec and TensorSpec is not allowed.The dataset represented by a schema can be named, with unique non empty names for every input. In the case of
ColSpec
, the dataset columns can be unnamed with implicit integer index defined by their list indices. Combination of named and unnamed data inputs are not allowed.-
as_spark_schema
()[source] Convert to Spark schema. If this schema is a single unnamed column, it is converted directly the corresponding spark data type, otherwise it’s returned as a struct (missing column names are filled with an integer sequence). Unsupported by TensorSpec.
-
classmethod
from_json
(json_str: str)[source] Deserialize from a json string.
-
has_input_names
() → bool[source] Return true iff this schema declares names, false otherwise.
-
input_dict
() → dict[source] Maps column names to inputs, iff this schema declares names.
-
input_names
() → list[source] Get list of data names or range of indices if the schema has no names.
-
input_types
() → list[source] Get types for each column in the schema.
-
input_types_dict
() → dict[source] Maps column names to types, iff this schema declares names.
-
is_tensor_spec
() → bool[source] Return true iff this schema is specified using TensorSpec
-
numpy_types
() → list[source] Convenience shortcut to get the datatypes as numpy types.
-
optional_input_names
() → list[source] Note
Experimental: This function may change or be removed in a future release without warning.
Get list of optional data names or range of indices if schema has no names.
-
pandas_types
() → list[source] Convenience shortcut to get the datatypes as pandas types. Unsupported by TensorSpec.
-
required_input_names
() → list[source] Get list of required data names or range of indices if schema has no names.
-
to_dict
() → list[source] Serialize into a jsonable dictionary.
-
to_json
() → str[source] Serialize into json string.
-
-
class
mlflow.types.
TensorSpec
(type: numpy.dtype, shape: Union[tuple, list], name: Optional[str] = None)[source] Bases:
object
Specification used to represent a dataset stored as a Tensor.
-
classmethod
from_json_dict
(**kwargs)[source] Deserialize from a json loaded dictionary. The dictionary is expected to contain type and tensor-spec keys.
-
property
required
Whether this tensor is required.
Note
Experimental: This property may change or be removed in a future release without warning.
-
property
type
A unique character code for each of the 21 different numpy built-in types. See https://numpy.org/devdocs/reference/generated/numpy.dtype.html#numpy.dtype for details.
-
classmethod
-
class
mlflow.types.llm.
ChatChoice
(message: mlflow.types.llm.ChatMessage, index: int = 0, finish_reason: str = 'stop', logprobs: Optional[mlflow.types.llm.ChatChoiceLogProbs] = None)[source] A single chat response generated by the model. ref: https://platform.openai.com/docs/api-reference/chat/object
- Parameters
message (
ChatMessage
) – The message that was generated.index (int) – The index of the response in the list of responses. Defaults to
0
finish_reason (str) – The reason why generation stopped. Optional, defaults to
"stop"
logprobs (
ChatChoiceLogProbs
) – Log probability information for the choice. Optional, defaults toNone
-
class
mlflow.types.llm.
ChatChoiceDelta
(role: Optional[str] = 'assistant', content: Optional[str] = None, refusal: Optional[str] = None, name: Optional[str] = None, tool_calls: Optional[list] = None)[source] A streaming message delta in a chat response.
- Parameters
role (str) – The role of the entity that sent the message (e.g.
"user"
,"system"
,"assistant"
,"tool"
). Optional defaults to"assistant"
This is optional because OpenAI clients can explicitly return None for the rolecontent (str) – The content of the new token being streamed Optional Can be
None
on the last delta chunk or if refusal or tool_calls are providedrefusal (str) – The refusal message content. Optional Supplied if a refusal response is provided.
name (str) – The name of the entity that sent the message. Optional.
tool_calls (List[
ToolCall
]) – A list of tool calls made by the model. Optional defaults toNone
-
class
mlflow.types.llm.
ChatChoiceLogProbs
(content: Optional[list] = None)[source] Log probability information for the choice.
- Parameters
content – A list of message content tokens with log probability information.
-
class
mlflow.types.llm.
ChatChunkChoice
(delta: mlflow.types.llm.ChatChoiceDelta, index: int = 0, finish_reason: Optional[str] = None, logprobs: Optional[mlflow.types.llm.ChatChoiceLogProbs] = None)[source] A single chat response chunk generated by the model. ref: https://platform.openai.com/docs/api-reference/chat/streaming
- Parameters
index (int) – The index of the response in the list of responses. defaults to
0
delta (
ChatChoiceDelta
) – The streaming chunk message that was generated.finish_reason (str) – The reason why generation stopped. Optional, defaults to
None
logprobs (
ChatChoiceLogProbs
) – Log probability information for the choice. Optional, defaults toNone
-
class
mlflow.types.llm.
ChatCompletionChunk
(choices: list, usage: Optional[mlflow.types.llm.TokenUsageStats] = None, id: Optional[str] = None, model: Optional[str] = None, object: str = 'chat.completion.chunk', created: int = <factory>, custom_outputs: Optional[dict] = None)[source] The streaming chunk returned by the chat endpoint. ref: https://platform.openai.com/docs/api-reference/chat/streaming
- Parameters
choices (List[
ChatChunkChoice
]) – A list ofChatChunkChoice
objects containing the generated chunk of a streaming responseusage (
TokenUsageStats
) – An object describing the tokens used by the request. Optional, defaults toNone
.id (str) – The ID of the response. Optional, defaults to
None
model (str) – The name of the model used. Optional, defaults to
None
object (str) – The object type. Defaults to ‘chat.completion.chunk’
created (int) – The time the response was created. Optional, defaults to the current time.
custom_outputs (Dict[str, Any]) – An field that can contain arbitrary additional context. The dictionary values must be JSON-serializable. Optional, defaults to
None
-
class
mlflow.types.llm.
ChatCompletionRequest
(temperature: float = 1.0, max_tokens: Optional[int] = None, stop: Optional[list] = None, n: int = 1, stream: bool = False, top_p: Optional[float] = None, top_k: Optional[int] = None, frequency_penalty: Optional[float] = None, presence_penalty: Optional[float] = None, custom_inputs: Optional[dict] = None, tools: Optional[list] = None, messages: list = <factory>)[source] Format of the request object expected by the chat endpoint.
- Parameters
messages (List[
ChatMessage
]) – A list ofChatMessage
that will be passed to the model. Optional, defaults to empty list ([]
)temperature (float) – A param used to control randomness and creativity during inference. Optional, defaults to
1.0
max_tokens (int) – The maximum number of new tokens to generate. Optional, defaults to
None
(unlimited)stop (List[str]) – A list of tokens at which to stop generation. Optional, defaults to
None
n (int) – The number of responses to generate. Optional, defaults to
1
stream (bool) – Whether to stream back responses as they are generated. Optional, defaults to
False
top_p (float) – An optional param to control sampling with temperature, the model considers the results of the tokens with top_p probability mass. E.g., 0.1 means only the tokens comprising the top 10% probability mass are considered.
top_k (int) – An optional param for reducing the vocabulary size to top k tokens (sorted in descending order by their probabilities).
frequency_penalty – (float): An optional param of positive or negative value, positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.
presence_penalty – (float): An optional param of positive or negative value, positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.
custom_inputs (Dict[str, Any]) – An optional param to provide arbitrary additional context to the model. The dictionary values must be JSON-serializable.
tools (List[
ToolDefinition
]) – An optional list of tools that can be called by the model.
Warning
In an upcoming MLflow release, default values for temperature, n and stream will be removed. Please provide these values explicitly in your code if needed.
-
class
mlflow.types.llm.
ChatCompletionResponse
(choices: list, usage: Optional[mlflow.types.llm.TokenUsageStats] = None, id: Optional[str] = None, model: Optional[str] = None, object: str = 'chat.completion', created: int = <factory>, custom_outputs: Optional[dict] = None)[source] The full response object returned by the chat endpoint.
- Parameters
choices (List[
ChatChoice
]) – A list ofChatChoice
objects containing the generated responsesusage (
TokenUsageStats
) – An object describing the tokens used by the request. Optional, defaults toNone
.id (str) – The ID of the response. Optional, defaults to
None
model (str) – The name of the model used. Optional, defaults to
None
object (str) – The object type. Defaults to ‘chat.completion’
created (int) – The time the response was created. Optional, defaults to the current time.
custom_outputs (Dict[str, Any]) – An field that can contain arbitrary additional context. The dictionary values must be JSON-serializable. Optional, defaults to
None
-
class
mlflow.types.llm.
ChatMessage
(role: str, content: Optional[str] = None, refusal: Optional[str] = None, name: Optional[str] = None, tool_calls: Optional[list] = None, tool_call_id: Optional[str] = None)[source] A message in a chat request or response.
- Parameters
role (str) – The role of the entity that sent the message (e.g.
"user"
,"system"
,"assistant"
,"tool"
).content (str) – The content of the message. Optional Can be
None
if refusal or tool_calls are provided.refusal (str) – The refusal message content. Optional Supplied if a refusal response is provided.
name (str) – The name of the entity that sent the message. Optional.
tool_calls (List[
ToolCall
]) – A list of tool calls made by the model. Optional defaults toNone
tool_call_id (str) – The ID of the tool call that this message is a response to. Optional defaults to
None
-
class
mlflow.types.llm.
ChatParams
(temperature: float = 1.0, max_tokens: Optional[int] = None, stop: Optional[list] = None, n: int = 1, stream: bool = False, top_p: Optional[float] = None, top_k: Optional[int] = None, frequency_penalty: Optional[float] = None, presence_penalty: Optional[float] = None, custom_inputs: Optional[dict] = None, tools: Optional[list] = None)[source] Common parameters used for chat inference
- Parameters
temperature (float) – A param used to control randomness and creativity during inference. Optional, defaults to
1.0
max_tokens (int) – The maximum number of new tokens to generate. Optional, defaults to
None
(unlimited)stop (List[str]) – A list of tokens at which to stop generation. Optional, defaults to
None
n (int) – The number of responses to generate. Optional, defaults to
1
stream (bool) – Whether to stream back responses as they are generated. Optional, defaults to
False
top_p (float) – An optional param to control sampling with temperature, the model considers the results of the tokens with top_p probability mass. E.g., 0.1 means only the tokens comprising the top 10% probability mass are considered.
top_k (int) – An optional param for reducing the vocabulary size to top k tokens (sorted in descending order by their probabilities).
frequency_penalty – (float): An optional param of positive or negative value, positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.
presence_penalty – (float): An optional param of positive or negative value, positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.
custom_inputs (Dict[str, Any]) – An optional param to provide arbitrary additional context to the model. The dictionary values must be JSON-serializable.
tools (List[
ToolDefinition
]) – An optional list of tools that can be called by the model.
Warning
In an upcoming MLflow release, default values for temperature, n and stream will be removed. Please provide these values explicitly in your code if needed.
-
classmethod
keys
() → set[source] Return the keys of the dataclass
-
class
mlflow.types.llm.
FunctionToolCallArguments
(name: str, arguments: str)[source] The arguments of a function tool call made by the model.
- Parameters
arguments (str) – A JSON string of arguments that should be passed to the tool.
name (str) – The name of the tool that is being called.
-
class
mlflow.types.llm.
FunctionToolDefinition
(name: str, description: Optional[str] = None, parameters: Optional[mlflow.types.llm.ToolParamsSchema] = None, strict: bool = False)[source] Definition for function tools (currently the only supported type of tool).
- Parameters
name (str) – The name of the tool.
description (str) – A description of what the tool does, and how it should be used. Optional, defaults to
None
parameters – A mapping of parameter names to their definitions. If not provided, this defines a function without parameters. Optional, defaults to
None
strict (bool) – A flag that represents whether or not the model should strictly follow the schema provided.
-
to_tool_definition
()[source] Convenience function for wrapping this in a ToolDefinition
-
class
mlflow.types.llm.
ParamProperty
(type: Literal[string, number, integer, object, array, boolean, null], description: Optional[str] = None, enum: Optional[list] = None, items: Optional[mlflow.types.llm.ParamType] = None)[source] A single parameter within a function definition.
- Parameters
type (str) – The type of the parameter. Possible values are “string”, “number”, “integer”, “object”, “array”, “boolean”, or “null”, conforming to the JSON Schema specification.
description (str) – A description of the parameter. Optional, defaults to
None
enum (List[str]) – Used to constrain the possible values for the parameter. Optional, defaults to
None
items (
ParamProperty
) – If the param is ofarray
type, this field can be used to specify the type of its items. Optional, defaults toNone
-
class
mlflow.types.llm.
ParamType
(type: Literal[string, number, integer, object, array, boolean, null])[source]
-
class
mlflow.types.llm.
TokenLogProb
(token: str, logprob: float, top_logprobs: list, bytes: Optional[list] = None)[source] Message content token with log probability information.
- Parameters
token – The token.
logprob – The log probability of this token, if it is within the top 20 most likely tokens. Otherwise, the value -9999.0 is used to signify that the token is very unlikely.
bytes – A list of integers representing the UTF-8 bytes representation of the token. Useful in instances where characters are represented by multiple tokens and their byte representations must be combined to generate the correct text representation. Can be null if there is no bytes representation for the token.
top_logprobs – List of the most likely tokens and their log probability, at this token position. In rare cases, there may be fewer than the number of requested top_logprobs returned.
-
class
mlflow.types.llm.
TokenUsageStats
(prompt_tokens: Optional[int] = None, completion_tokens: Optional[int] = None, total_tokens: Optional[int] = None)[source] Stats about the number of tokens used during inference.
- Parameters
prompt_tokens (int) – The number of tokens in the prompt. Optional, defaults to
None
completion_tokens (int) – The number of tokens in the generated completion. Optional, defaults to
None
total_tokens (int) – The total number of tokens used. Optional, defaults to
None
-
class
mlflow.types.llm.
ToolCall
(function: mlflow.types.llm.FunctionToolCallArguments, id: str = <factory>, type: str = 'function')[source] A tool call made by the model.
- Parameters
function (
FunctionToolCallArguments
) – The arguments of the function tool call.id (str) – The ID of the tool call. Defaults to a random UUID.
type (str) – The type of the object. Defaults to “function”.
-
class
mlflow.types.llm.
ToolDefinition
(function: mlflow.types.llm.FunctionToolDefinition, type: Literal[function] = 'function')[source] Definition for tools that can be called by the model.
- Parameters
function (
FunctionToolDefinition
) – The definition of a function tool.type (str) – The type of the tool. Currently only “function” is supported.
-
class
mlflow.types.llm.
ToolParamsSchema
(properties: dict, type: Literal[object] = 'object', required: Optional[list] = None, additionalProperties: Optional[bool] = None)[source] A tool parameter definition.
- Parameters
properties (Dict[str,
ParamProperty
]) – A mapping of parameter names to their definitions.type (str) – The type of the parameter. Currently only “object” is supported.
required (List[str]) – A list of required parameter names. Optional, defaults to
None
additionalProperties (bool) – Whether additional properties are allowed in the object. Optional, defaults to
None
-
class
mlflow.types.llm.
TopTokenLogProb
(token: str, logprob: float, bytes: Optional[list] = None)[source] Token and its log probability.
- Parameters
token – The token.
logprob – The log probability of this token, if it is within the top 20 most likely tokens. Otherwise, the value -9999.0 is used to signify that the token is very unlikely.
bytes – A list of integers representing the UTF-8 bytes representation of the token. Useful in instances where characters are represented by multiple tokens and their byte representations must be combined to generate the correct text representation. Can be null if there is no bytes representation for the token.
-
class
mlflow.types.chat.
AudioContentPart
(*, type: Literal[input_audio], input_audio: mlflow.types.chat.InputAudio)[source]
-
class
mlflow.types.chat.
BaseModel
[source]
-
class
mlflow.types.chat.
ChatTool
(*, type: Literal[function, uc_function], function: Optional[mlflow.types.chat.FunctionToolDefinition] = None, uc_function: Optional[mlflow.types.chat.UnityCatalogFunctionToolDefinition] = None)[source]
-
class
mlflow.types.chat.
ChatTools
(*, tools: Optional[list] = None)[source]
-
class
mlflow.types.chat.
Function
(*, name: str, arguments: str)[source]
-
class
mlflow.types.chat.
FunctionParams
(*, properties: dict, type: Literal[object] = 'object', required: Optional[list] = None, additionalProperties: Optional[bool] = None)[source]
-
class
mlflow.types.chat.
FunctionToolDefinition
(*, name: str, description: Optional[str] = None, parameters: Optional[mlflow.types.chat.FunctionParams] = None, strict: Optional[bool] = None)[source]
-
class
mlflow.types.chat.
ImageContentPart
(*, type: Literal[image_url], image_url: mlflow.types.chat.ImageUrl)[source]
-
class
mlflow.types.chat.
ImageUrl
(*, url: str, detail: Literal[auto, low, high])[source]
-
class
mlflow.types.chat.
InputAudio
(*, data: str, format: Literal[wav, mp3])[source]
-
class
mlflow.types.chat.
ParamProperty
(*, type: Literal[string, number, integer, object, array, boolean, null], description: Optional[str] = None, enum: Optional[list] = None, items: Optional[mlflow.types.chat.ParamType] = None)[source]
-
class
mlflow.types.chat.
ParamType
(*, type: Literal[string, number, integer, object, array, boolean, null])[source]
-
class
mlflow.types.chat.
RequestMessage
(*, role: str, content: Optional[Union[str, list]] = None, tool_calls: Optional[list] = None, tool_call_id: Optional[str] = None, refusal: Optional[str] = None)[source] A chat request.
content
can be a string, or an array of content parts.A content part is one of the following:
-
class
mlflow.types.chat.
TextContentPart
(*, type: Literal[text], text: str)[source]
-
class
mlflow.types.chat.
ToolCall
(*, id: str, type: Literal[function], function: mlflow.types.chat.Function)[source]
-
class
mlflow.types.chat.
UnityCatalogFunctionToolDefinition
(*, name: str)[source]
-
class
mlflow.types.schema.
AnyType
[source] Note
Experimental: This class may change or be removed in a future release without warning.
-
to_dict
()[source] Dictionary representation of the object.
-
-
class
mlflow.types.schema.
Array
(dtype: Union[mlflow.types.schema.Array, mlflow.types.schema.DataType, mlflow.types.schema.Map, mlflow.types.schema.Object, mlflow.types.schema.AnyType, str])[source] Specification used to represent a json-convertible array.
-
classmethod
from_json_dict
(**kwargs)[source] Deserialize from a json loaded dictionary. The dictionary is expected to contain type and items keys. Example: {“type”: “array”, “items”: “string”}
-
to_dict
()[source] Dictionary representation of the object.
-
classmethod
-
class
mlflow.types.schema.
Map
(value_type: Union[mlflow.types.schema.Array, mlflow.types.schema.DataType, mlflow.types.schema.Map, mlflow.types.schema.Object, mlflow.types.schema.AnyType, str])[source] Specification used to represent a json-convertible map with string type keys.
-
classmethod
from_json_dict
(**kwargs)[source] Deserialize from a json loaded dictionary. The dictionary is expected to contain type and values keys. Example: {“type”: “map”, “values”: “string”}
-
to_dict
()[source] Dictionary representation of the object.
-
classmethod
-
class
mlflow.types.schema.
Object
(properties: list)[source] Specification used to represent a json-convertible object.
-
classmethod
from_json_dict
(**kwargs)[source] Deserialize from a json loaded dictionary. The dictionary is expected to contain type and properties keys. Example: {“type”: “object”, “properties”: {“property_name”: {“type”: “string”}}}
-
to_dict
()[source] Dictionary representation of the object.
-
classmethod
-
class
mlflow.types.schema.
Property
(name: str, dtype: Union[mlflow.types.schema.Array, mlflow.types.schema.DataType, mlflow.types.schema.Map, mlflow.types.schema.Object, mlflow.types.schema.AnyType, str], required: bool = True)[source] Specification used to represent a json-convertible object property.
-
classmethod
from_json_dict
(**kwargs)[source] Deserialize from a json loaded dictionary. The dictionary is expected to contain only one key as name, and the value should be a dictionary containing type and optional required keys. Example: {“property_name”: {“type”: “string”, “required”: True}}
-
to_dict
()[source] Dictionary representation of the object.
-
classmethod