Everett Kleven
10/07/2025, 6:08 PMEverett Kleven
10/07/2025, 6:14 PMfile function we have a expr.__eval__expressions("function_name", io_config=io_config) . Since I'm passing multiple expressions into my function.
For generate_text, I was initially thining of enabling the user to pass in a separate expression for each modality input so that each modality column clearly passable to the client requests for message input construction.
With some modalities like audio and files requiring io_config, I am now realizing I'd somehow need to pass in the io_config to each expression?
Here is what I have currently:
##
# GENERATE TEXT FUNCTIONS
##
# OpenAI Chat Completions Overload
@overload
def generate_text(
*,
# Multi-Modal Inputs + Dedicated History
text: Expression | None = None,
image: Expression | None = None,
audio: Expression | None = None,
file: Expression | None = None,
history: Expression | None = None,
provider: Literal["openai"] | OpenAIProvider | None,
model: str = "openai/gpt-5-nano",
# OpenAI Chat Completion Overload
oai_endpoint: Literal["chat"] | None = ...,
oai_output_mode: OpenAIOutputMode | None = "text-only",
json_schema: JsonSchema | None = None,
extra_body: OpenAIChatExtraBody | None = None,
# Daft UDF Kwargs
return_dtype: DataType = DataType.string(),
num_cpus: float = 1,
num_gpus: float = 0,
batch_size: int = 1,
concurrency: int = 1,
memory_bytes: int | None = None,
use_process: bool = False,
# IOConfig Kwargs
io_config: IOConfig | None = None,
# Model Options to Pass to Descriptor
**options,
) -> Expression: ...
# OpenAI Responses Overload
@overload
def generate_text(
*,
# Multi-Modal Inputs + Dedicated History
text: Expression | None = ...,
image: Expression | None = ...,
audio: Expression | None = ...,
file: Expression | None = ...,
history: Expression | None = ...,
# OpenAI Responses Overload
provider: Literal["openai"] | OpenAIProvider | None = ...,
model: str = ...,
oai_endpoint: Literal["responses"] | None = ...,
oai_output_mode: OpenAIOutputMode | None = ...,
json_schema: JsonSchema | None = ...,
extra_body: OpenAIResponsesExtraBody | None = ...,
# Daft UDF Kwargs
return_dtype: DataType = ...,
num_cpus: float = ...,
num_gpus: float = ...,
batch_size: int = ...,
concurrency: int = ...,
memory_bytes: int | None = ...,
use_process: bool = ...,
# IOConfig Kwargs
io_config: IOConfig | None = ...,
# Model Options to Pass to Descriptor
**options,
) -> Expression: ...
# Common Overload
def generate_text(
*,
# Multi-Modal Inputs + Dedicated History
text: Expression | None = None,
image: Expression | None = None,
audio: Expression | None = None,
file: Expression | None = None,
history: Expression | None = None,
# Common Request Kwargs
provider: str | Provider | None = "openai",
model: str = "openai/gpt-5-nano",
extra_body: dict[str, Any] | OpenAIChatExtraBody | OpenAIResponsesExtraBody | None = None,
# OpenAI Overload Kwargs
json_schema: JsonSchema | None = None,
oai_endpoint: OpenAIEndpoint | None = None,
oai_output_mode: OpenAIOutputMode | None = None,
# Daft UDF Kwargs
return_dtype: DataType = DataType.string(),
num_cpus: float = 1,
num_gpus: float = 0,
batch_size: int = 1,
concurrency: int = 1,
memory_bytes: int | None = None,
use_process: bool = False,
# IOConfig for Chat Input Construction
io_config: IOConfig | None = None,
# Any Additional Options to Pass to the model initializer
**options,
) -> Expression:
"""Returns an expression that generates text using the specified model and provider.
Args:
text (String Expression | List[String] Expression | None): The input text column expression.
image (Image Expression | List[Image] Expression | None): The input image column expression.
audio (Audio Expression | List[Audio] Expression | None): The input audio column expression.
file (File Expression | List[File] Expression | None): The input file column expression.
history (String Expression | None): The input history column expression.
provider (str | Provider | None): The provider to use for the text generation.
model (str): The model to use for the text generation.
extra_body (Options | None): The extra body to pass to the text generation request.
OpenAI Request Kwargs:
oai_endpoint (OpenAIEndpoint | None): The oai endpoint to pass to the text generation request.
oai_output_mode (OpenAIOutputMode | None): The oai output mode to pass to the text generation request.
json_schema (JsonSchema | None): A convenience argument to pass json schema to either the chat or responses text generation structured outputs request.
extra_body (OpenAIChatCompletionExtraBody | OpenAIResponsesExtraBody | None): The extra body to pass to the text generation request.
UDF Kwargs:
return_dtype (DataType): The return dtype to pass to the text generation request.
num_cpus (float): The number of cpus to pass to the text generation request.
num_gpus (float): The number of gpus to pass to the text generation request.
batch_size (int): The batch size to pass to the text generation request.
concurrency (int): The concurrency to pass to the text generation request.
memory_bytes (int | None): The memory bytes to pass to the text generation request.
use_process (bool): The use process to pass to the text generation request.
Other Kwargs:
io_config (IOConfig | None): The io config to pass to the text generation request.
**options (Any): Any additional options to pass to the model initializer.
Simple Usage:
- OpenAI (Chat Completions):
>>> df = daft.read_csv("data.csv")
>>> df = df.with_column(
generate_text(
text=df["text"],
model="openai/gpt-5-nano",
provider="openai",
oai_endpoint="chat",
))
>>> df.show()
- OpenAI (Responses):
>>> df = daft.read_csv("data.csv")
>>> df = df.with_column(
generate_text(
text=df["text"],
model="openai/gpt-5-nano",
provider="openai",
oai_endpoint="responses",
))
>>> df.show()
Advanced Usage:
- OpenAI (Multimodal Chat Completions):
>>> df = daft.read_csv("data.csv")
>>> df = df.with_column(
generate_text(
text=df["text"],
image=df["image"],
audio=df["audio"],
file=df["file"],
history=df["history"],
model="openai/gpt-5-nano",
provider="openai",
oai_endpoint="chat",
))
>>> df.show()
- OpenAI (Responses) with structured outputs:
>>> df = daft.read_csv("data.csv")
>>> df = df.with_column(
generate_text(
text=df["text"],
image=df["image"],
file=df["file"],
history=df["history"],
model="openai/gpt-5-nano",
provider="openai",
oai_endpoint="responses",
json_schema=MyPydanticModel.model_json_schema(),
))
>>> df.show()
"""
from daft.ai._expressions import _TextGenerationExpression
from daft.ai.protocols import TextGenerator
# NOTE: Because we are using overloads, we seperate inference request specific kwargs from model initializer kwargs
model_options_superset = {
"oai_endpoint": oai_endpoint,
"oai_output_mode": oai_output_mode,
**options,
}
model_options = {k: v for k, v in model_options_superset.items() if v is not None}
# Attribute Extra Body Overload Kwargs
extra_body_superset = {
"extra_body": extra_body,
"json_schema": json_schema,
}
extra_body_options = {k: v for k, v in extra_body_superset.items() if v is not None}
# get the text generator descriptor
text_generator = _resolve_provider(provider, "openai").get_text_generator(model, **model_options)
# Create a class-based batch UDF
expr_callable = udf(
return_dtype=return_dtype,
num_cpus=num_cpus,
num_gpus=num_gpus,
batch_size=batch_size,
concurrency=concurrency,
memory_bytes=memory_bytes,
use_process=use_process,
)
# Turn the udf into a Text Generation Expression
expr = expr_callable(_TextGenerationExpression)
# Initialize the Text Generation Expression UDF with the descriptor from the provider
expr = expr.with_init_args(text_generator)
# Perform the Text Generation Request with extra request body options and io config
return expr(
text_series=text,
image_series=image,
audio_series=audio,
file_series=file,
history_series=history,
model=model,
extra_body=extra_body_options,
io_config=io_config,
)Robert Howell
10/07/2025, 6:15 PMEverett Kleven
10/07/2025, 6:17 PM