Daft
Daft copied to clipboard
bug: Provider string reference isn't enough to resolve custom provider name.
Describe the bug
When passing custom provider name as provider arg in Prompt, if you specify just the custom provider name in an ai function argument, resolve_provider misses the resolution and load_provider throws a ValueError: Provider 'OpenRouter' is not yet supported.
To Reproduce
# /// script
# description = "Prompt an OpenRouter model using a session"
# requires-python = ">=3.10, <3.13"
# dependencies = ["daft","openai","pydantic","python-dotenv"]
# ///
import os
from dotenv import load_dotenv
import daft
from daft.ai.openai.provider import OpenAIProvider
from daft.functions.ai import prompt
from daft.session import Session
if __name__ == "__main__":
# Load environment variables
load_dotenv()
# Create an OpenRouter provider
openrouter_provider = OpenAIProvider(
name="OpenRouter",
base_url="https://openrouter.ai/api/v1",
api_key=os.environ.get("OPENROUTER_API_KEY")
)
# Create a session and attach the provider
sess = Session()
sess.attach_provider(openrouter_provider)
sess.set_provider("OpenRouter")
# Create a dataframe with the quotes
df = daft.from_pydict({
"quote": [
"I am going to be the king of the pirates!",
"I'm going to be the next Hokage!",
],
})
# Use the prompt function to classify the quotes
df = (
df
.with_column(
"nemotron-response",
prompt(
daft.col("quote"),
system_message="You are an anime expert. Classify the anime based on the text and returns the name, character, and quote.",
provider="OpenRouter", # but sess.get_provider("OpenRouter") works
model="nvidia/nemotron-nano-9b-v2:free"
)
)
)
df.show(format="fancy", max_width=120)
Expected behavior
Should be a small update to _resolve_provider() in daft.functions.ai.init.py
Component(s)
Other
Additional context
THESE OTHER METHODS WORK
If connecting to OpenAI, users can naively specify "openai" to have an OpenAIProvider created and attached for them.
# /// script
# description = "Embed Video Frames from a Youtube Video"
# requires-python = ">=3.10, <3.13"
# dependencies = ["daft","openai","pydantic","python-dotenv"]
# ///
from dotenv import load_dotenv
import daft
from daft.functions.ai import prompt
if __name__ == "__main__":
# Load environment variables
load_dotenv()
# Create a dataframe with the quotes
df = daft.from_pydict({
"quote": [
"I am going to be the king of the pirates!",
"I'm going to be the next Hokage!",
],
})
# Use the prompt function to classify the quotes
df = (
df
.with_column(
"response",
prompt(
daft.col("quote"),
system_message="You are an anime expert. Classify the anime based on the text and returns the name, character, and quote.",
provider="openai", # Automatically creates an OpenAI provider
model="gpt-5-nano"
)
)
)
df.show(format="fancy", max_width=120)
Passing Custom Provider Object Directly (ie OpenRouter)
# /// script
# description = "Prompt an OpenRouter model"
# requires-python = ">=3.10, <3.13"
# dependencies = ["daft","openai","pydantic","python-dotenv"]
# ///
import os
from dotenv import load_dotenv
import daft
from daft.ai.openai.provider import OpenAIProvider
from daft.functions.ai import prompt
if __name__ == "__main__":
# Load environment variables
load_dotenv()
# Create an OpenRouter provider
openrouter_provider = OpenAIProvider(
name="OpenRouter",
base_url="https://openrouter.ai/api/v1",
api_key=os.environ.get("OPENROUTER_API_KEY")
)
# Create a dataframe with the quotes
df = daft.from_pydict({
"quote": [
"I am going to be the king of the pirates!",
"I'm going to be the next Hokage!",
],
})
# Use the prompt function to classify the quotes
df = (
df
.with_column(
"response",
prompt(
daft.col("quote"),
system_message="You are an anime expert. Classify the anime based on the text and returns the name, character, and quote.",
provider=openrouter_provider,
model="nvidia/nemotron-nano-9b-v2:free"
)
)
)
df.show(format="fancy", max_width=120)
@rchowell