Replies: 2 comments
-
@austinmw could you try creating the tool manually and setting the we're also thinking of adding a more general |
Beta Was this translation helpful? Give feedback.
-
@vbarda I'm actually not using One thing that complicates my workflow a bit is that I want my Pydantic schema to be dynamically created based on information gathered from a user. I'm currently doing it like this: # Function to dynamically create a Pydantic BaseModel schema
def create_workout_plan_template(num_weeks: int = 2, max_workouts_per_day: int = 1) -> Type[BaseModel]:
"""Dynamically create a generic workout plan template based on num_weeks and max_workouts_per_day input by user."""
# Nested pydantic classes omitted for brevity (e.g. `Week`)
class WorkoutPlan(BaseModel):
"""Multi-week workout program."""
explanation: str = Field(
min_length=30,
description="Highly intelligent and knowledgeable (but very polite) reasoning/rationale for the workout program design.",
)
weeks: list[Week] = Field(description=f"List of exactly {num_weeks} weeks to structure the workout program.")
@model_validator(mode='after')
def validate_weeks_count(self) -> 'WorkoutPlan':
actual_weeks = len(self.weeks)
if actual_weeks != num_weeks:
raise ValueError(
f"Invalid number of workout weeks. Expected {num_weeks} weeks, but got {actual_weeks} weeks. "
"Try the tool call again!"
)
return self
return WorkoutPlan
# Function to return a tools list from a Pydantic BaseModel definition
def create_workout_plan_tools(workout_plan_template):
"""Create a list of tools (just one in this case)."""
def create_workout_plan(request: workout_plan_template) -> dict:
"""Return a generated workout program"""
return request.model_dump()
tools = (create_workout_plan,)
return tools
# Dynamically create Pydantic model by passing num_weeks obtained from user
workout_plan_template = create_workout_plan_template(num_weeks)
# Create list of tools
tools = create_workout_plan_tools(workout_plan_template)
# Create a tool node for use in my graph
tool_node = ToolNode(tools)
# Conditional edge based on tool node output:
def should_retry_plan(state: PlannerState) -> Literal["Success", "Failure"]:
"""Decide whether to send error to agent or tool response to parser"""
messages = state["messages"]
last_message = messages[-1]
if "error" in last_message.content.lower():
return "Failure"
return "Success" |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm using
ToolNode
, which wraps a tool that takes as input a Pydantic BaseModel.Currently when my LLM fails to generate the correct schema, the ToolNode fails with an error like this:
This error is sent back to the LLM to help provide information for a better retry attempt, but the error doesn't really provide enough information to help the LLM. How can I replace this with a concatenation of the actual underlying validation errors?
Also my second question is, since my tool is simply this:
Should I even be using
ToolNode
, or is something else more fitting likeValidationNode
?Beta Was this translation helpful? Give feedback.
All reactions