Get startedGet started for free

Say More, Agent: Enforcing Answers Length

You're building a real estate assistant that helps users find homes based on their preferences. Sometimes, the agent gives overly brief or vague recommendations, like "You should check out this house in Brooklyn."

To make responses more helpful, you want to validate that each answer contains at least 200 characters. If it's too short, the agent should try again using your error message as guidance.

In this exercise, you'll write a validation function that checks the length of a response.

This exercise is part of the course

AI Agents with Hugging Face smolagents

View Course

Exercise instructions

  • Check if the final_answer is shorter than 200 characters. If it is, raise an exception with a helpful error message.
  • If it passes the check, return True to mark the answer as valid.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

def check_answer_length(final_answer, agent_memory):
    # Check if answer contains less than 200 characters
    if len(str(final_answer)) < ____:
        raise Exception("The answer is too short. Please include more details.")
    # Return True if check passes
    return ____
Edit and Run Code