-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ChatBedrockConverse Pydantic example working with Haiku but not with Sonnet? #125
Comments
@austinmw It seems like what you are doing here might be accomplished with the |
Here is a slight variation of your code that works with both sonnet and haiku models. from langchain_aws import ChatBedrockConverse
llm = ChatBedrockConverse(
model_id="anthropic.claude-3-sonnet-20240229-v1:0", # Change to Haiku to see it work
#model_id="anthropic.claude-3-haiku-20240307-v1:0",
max_tokens=4096,
)
import json
from typing import Any
from langchain.pydantic_v1 import BaseModel, Field
# Model definitions
class Day(BaseModel):
day_of_week: str = Field(..., description="Day of the week")
activities: list[str] = Field(..., description="List of 1-3 high-level workout descriptions or rest day activities")
class Week(BaseModel):
monday: Day
tuesday: Day
wednesday: Day
thursday: Day
friday: Day
saturday: Day
sunday: Day
class WorkoutProgram(BaseModel):
"""Generate a 4-week workout program for a beginner"""
week1: Week = Field(..., description="Workout plan for Week 1")
week2: Week = Field(..., description="Workout plan for Week 2")
week3: Week = Field(..., description="Workout plan for Week 3")
week4: Week = Field(..., description="Workout plan for Week 4")
llm_with_structure = llm.with_structured_output(WorkoutProgram)
# Workout program generation
def generate_initial_program(user_profile: dict) -> WorkoutProgram:
initial_prompt = f"""
You are a professional fitness trainer tasked with creating a 4-week workout program for a beginner with the following characteristics:
{json.dumps(user_profile, indent=2)}
Create a program that is tailored to this individual's needs and goals. The program should focus on full-body workouts 3 days a week, but adjust the intensity and types of exercises based on the user's profile.
Include all 7 days in each week, with the non-workout days being rest days or light activity days.
For each day, provide 1-3 high-level descriptions of the workouts or activities, such as "30 min HIIT cardio" or "Chest and Back training".
Rest days can include suggestions like "Light stretching" or "20 min walk".
Please create this personalized 4-week workout program in a single response, providing a complete 4-week plan.
"""
response: Any = llm_with_structure.invoke(initial_prompt)
return response
user_profile = {
"height": "5'10\"",
"weight": "180 lbs",
"age": 30,
"goals": "Lose weight and improve overall fitness"
}
initial_workout_program = generate_initial_program(user_profile)
print("\nSuccessfully generated workout program:")
print(initial_workout_program.json(indent=2)) Output
|
Hi @3coins , I've just run the exact code you provided and actually still get a similar error. I've double checked that I'm using the latest version of Input code: from langchain_aws import ChatBedrockConverse
llm = ChatBedrockConverse(
model_id="anthropic.claude-3-sonnet-20240229-v1:0", # Change to Haiku to see it work
#model_id="anthropic.claude-3-haiku-20240307-v1:0",
max_tokens=4096,
)
import json
from typing import Any
from langchain.pydantic_v1 import BaseModel, Field
# Model definitions
class Day(BaseModel):
day_of_week: str = Field(..., description="Day of the week")
activities: list[str] = Field(..., description="List of 1-3 high-level workout descriptions or rest day activities")
class Week(BaseModel):
monday: Day
tuesday: Day
wednesday: Day
thursday: Day
friday: Day
saturday: Day
sunday: Day
class WorkoutProgram(BaseModel):
"""Generate a 4-week workout program for a beginner"""
week1: Week = Field(..., description="Workout plan for Week 1")
week2: Week = Field(..., description="Workout plan for Week 2")
week3: Week = Field(..., description="Workout plan for Week 3")
week4: Week = Field(..., description="Workout plan for Week 4")
llm_with_structure = llm.with_structured_output(WorkoutProgram)
# Workout program generation
def generate_initial_program(user_profile: dict) -> WorkoutProgram:
initial_prompt = f"""
You are a professional fitness trainer tasked with creating a 4-week workout program for a beginner with the following characteristics:
{json.dumps(user_profile, indent=2)}
Create a program that is tailored to this individual's needs and goals. The program should focus on full-body workouts 3 days a week, but adjust the intensity and types of exercises based on the user's profile.
Include all 7 days in each week, with the non-workout days being rest days or light activity days.
For each day, provide 1-3 high-level descriptions of the workouts or activities, such as "30 min HIIT cardio" or "Chest and Back training".
Rest days can include suggestions like "Light stretching" or "20 min walk".
Please create this personalized 4-week workout program in a single response, providing a complete 4-week plan.
"""
response: Any = llm_with_structure.invoke(initial_prompt)
return response
user_profile = {
"height": "5'10\"",
"weight": "180 lbs",
"age": 30,
"goals": "Lose weight and improve overall fitness"
}
initial_workout_program = generate_initial_program(user_profile)
print("\nSuccessfully generated workout program:")
print(initial_workout_program.json(indent=2)) Output I'm getting:
|
I'll close this issue. It appears that it happens because Claude may not always return a response that exactly matches the input schema, leading to failure in pydantic validation. |
@austinmw |
Pydantic WorkoutProgram Class Generation Fails with Claude 3 Sonnet but Works with Haiku
Description
When using the
ChatBedrockConverse
class from LangChain to generate a workout program, the code works fine with the Claude 3 Haiku model but fails with the Claude 3 Sonnet model. The error suggests that the Sonnet model is not returning the expected structure for theWorkoutProgram
. Using langchain-aws v0.1.12.Steps to Reproduce
Run the following self-contained script. Uncomment Haiku model_id to see it run successfully.
Code
Error Message
When run with the Sonnet model, you should see an error like this:
Any idea why pydantic validation fails for this code when I switch to Sonnet, but it works fine with Haiku?
The text was updated successfully, but these errors were encountered: