Skip to content

BadRequestError while trying out input guardrails #129

Open
@antonysamuel

Description

@antonysamuel

Please read this first

  • Have you read the docs?Agents SDK docs YES
  • Have you searched for related issues? Others may have had similar requesrs YES

Question

Got BadRequestError while trying to create and input guardrail.

Model Used: Groq

async_groq = AsyncOpenAI(
    api_key= os.getenv("GROQ_API_KEY"),
    base_url= "https://api.groq.com/openai/v1"
)
groq_model = OpenAIChatCompletionsModel(
        model= "llama-3.3-70b-versatile",
        openai_client= async_groq,
        
        
    ) 

from pydantic import BaseModel
from agents import (
    Agent,
    GuardrailFunctionOutput,
    InputGuardrailTripwireTriggered,
    RunContextWrapper,
    Runner,
    TResponseInputItem,
    input_guardrail,
)

class MathHomeworkOutput(BaseModel):
    is_math_homework: bool
    reasoning: str

guardrail_agent = Agent( 
    name="Guardrail check",
    instructions="Check if the user is asking you to do their math homework.",
    output_type=MathHomeworkOutput,
    model = groq_model
)


@input_guardrail
async def math_guardrail( 
    ctx: RunContextWrapper[None], agent: Agent, input: str | list[TResponseInputItem]
) -> GuardrailFunctionOutput:
    result = await Runner.run(guardrail_agent, input, context=ctx.context)

    return GuardrailFunctionOutput(
        output_info=result.final_output, 
        tripwire_triggered=result.final_output.is_math_homework,
    )


agent = Agent(  
    name="Customer support agent",
    instructions="You are a customer support agent. You help customers with their questions.",
    input_guardrails=[math_guardrail],
    model = groq_model
)

async def main():
    # This should trip the guardrail
    try:
        await Runner.run(agent, "Hello, can you help me solve for x: 2x + 3 = 11?")
        print("Guardrail didn't trip - this is unexpected")

    except InputGuardrailTripwireTriggered:
        print("Math homework guardrail tripped")

BadRequestError                           Traceback (most recent call last)
Cell In[102], [line 45](vscode-notebook-cell:?execution_count=102&line=45)
     [36](vscode-notebook-cell:?execution_count=102&line=36) agent = Agent(  
     [37](vscode-notebook-cell:?execution_count=102&line=37)     name="Customer support agent",
     [38](vscode-notebook-cell:?execution_count=102&line=38)     instructions="You are a customer support agent. You help customers with their questions.",
     [39](vscode-notebook-cell:?execution_count=102&line=39)     input_guardrails=[math_guardrail],
     [40](vscode-notebook-cell:?execution_count=102&line=40)     model= groq_model
     [41](vscode-notebook-cell:?execution_count=102&line=41) )
     [44](vscode-notebook-cell:?execution_count=102&line=44) try:
---> [45](vscode-notebook-cell:?execution_count=102&line=45)     result = await Runner.run(agent, "Hello, can you help me solve for x: 2x + 3 = 11?")
     [46](vscode-notebook-cell:?execution_count=102&line=46)     print("Guardrail didn't trip - this is unexpected")
     [48](vscode-notebook-cell:?execution_count=102&line=48) except InputGuardrailTripwireTriggered:

File /mnt/d/codes/llm/openai_agents/app1/.venv/lib/python3.12/site-packages/agents/run.py:210, in Runner.run(cls, starting_agent, input, context, max_turns, hooks, run_config)
    [205](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/codes/llm/openai_agents/app1/.venv/lib/python3.12/site-packages/agents/run.py:205) logger.debug(
    [206](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/codes/llm/openai_agents/app1/.venv/lib/python3.12/site-packages/agents/run.py:206)     f"Running agent {current_agent.name} (turn {current_turn})",
    [207](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/codes/llm/openai_agents/app1/.venv/lib/python3.12/site-packages/agents/run.py:207) )
    [209](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/codes/llm/openai_agents/app1/.venv/lib/python3.12/site-packages/agents/run.py:209) if current_turn == 1:
--> [210](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/codes/llm/openai_agents/app1/.venv/lib/python3.12/site-packages/agents/run.py:210)     input_guardrail_results, turn_result = await asyncio.gather(
    [211](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/codes/llm/openai_agents/app1/.venv/lib/python3.12/site-packages/agents/run.py:211)         cls._run_input_guardrails(
    [212](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/codes/llm/openai_agents/app1/.venv/lib/python3.12/site-packages/agents/run.py:212)             starting_agent,
    [213](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/codes/llm/openai_agents/app1/.venv/lib/python3.12/site-packages/agents/run.py:213)             starting_agent.input_guardrails
    [214](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/codes/llm/openai_agents/app1/.venv/lib/python3.12/site-packages/agents/run.py:214)             + (run_config.input_guardrails or []),
    [215](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/codes/llm/openai_agents/app1/.venv/lib/python3.12/site-packages/agents/run.py:215)             copy.deepcopy(input),
...
   (...)
   [1570](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/codes/llm/openai_agents/app1/.venv/lib/python3.12/site-packages/openai/_base_client.py:1570)     retries_taken=retries_taken,
   [1571](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/codes/llm/openai_agents/app1/.venv/lib/python3.12/site-packages/openai/_base_client.py:1571) )

BadRequestError: Error code: 400 - {'error': {'message': "'response_format.type' : value is not one of the allowed values ['text','json_object']", 'type': 'invalid_request_error'}}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions