Agent Foundry
All Problems

#66. Topic Restriction Guard

MediumGuardrails

The Problem

You built a financial advisor agent that should only answer questions about investments, stocks, budgeting, and personal finance. In practice, users ask it about everything — medical symptoms, legal advice, cooking recipes — and the agent happily obliges because there is no topic enforcement. The system prompt says "financial advisor" but the LLM doesn't reliably refuse off-topic queries on its own. Your job is to add a topic guard that checks whether the user's question falls within the finance domain and politely redirects off-topic requests.

Examples

Example 1

User input: What are the symptoms of diabetes?

Current (bad) output: The agent provides a detailed medical answer about diabetes symptoms, acting as a doctor.

Expected (good) output: "I'm a financial advisor and can only help with finance-related questions. For medical concerns, please consult a healthcare professional."

Example 2

User input: What's the best way to invest $10,000?

Current (bad) output: (This is fine — an on-topic question gets a normal answer.)

Expected (good) output: The agent provides investment advice as expected, since this is within its domain.

Example 3

User input: Write me a poem about the ocean

Current (bad) output: The agent writes a creative poem, completely outside its finance scope.

Expected (good) output: "I appreciate the creative request, but I'm specialized in financial advice. For creative writing, you might try a general-purpose assistant."

Your Task

Add a topic restriction guard so the agent:

  • Checks whether the user's query is related to finance before processing it.
  • Politely declines off-topic questions with a redirect suggestion.
  • Continues to answer finance-related questions normally.
  • Does not rely solely on the system prompt for topic enforcement.

Evaluation

Submissions are checked for the following:

  • Off-topic queries are rejected: Questions outside the finance domain are politely declined.
  • On-topic queries are answered: Finance-related questions still get helpful answers.
  • Polite redirect provided: Off-topic rejections include a suggestion to seek help elsewhere.

Constraints

  • The agent must only respond to questions within its designated domain (finance)
  • Off-topic queries must be politely declined with a redirect suggestion
  • Topic detection must happen before the LLM generates a full response
  • The system prompt alone is not a sufficient guardrail
Starter Code
from agents import Agent, Runner
from agents.tool import function_tool

@function_tool
def get_stock_price(ticker: str) -> str:
    """Get the current stock price for a ticker symbol."""
    prices = {"AAPL": "$185.23", "GOOGL": "$142.56", "MSFT": "$378.91"}
    return prices.get(ticker, f"No data for {ticker}")

# BUG: No topic enforcement — the financial advisor answers any question
agent = Agent(
    name="Financial Advisor",
    instructions="You are a financial advisor. Help users with investment and finance questions.",
    tools=[get_stock_price],
)

# Test: The agent happily answers a medical question
result = Runner.run_sync(agent, "What are the symptoms of diabetes?")
print(result.final_output)
Open in Google Colab
Evaluation Criteria0/3