Skip to content
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

BadRequestError while trying out input guardrails #129

Open
antonysamuel opened this issue Mar 13, 2025 · 1 comment
Open

BadRequestError while trying out input guardrails #129

antonysamuel opened this issue Mar 13, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@antonysamuel
Copy link

antonysamuel commented Mar 13, 2025

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'}}
@antonysamuel antonysamuel added the question Question about using the SDK label Mar 13, 2025
@rm-openai
Copy link
Collaborator

Looks like Groq doesn't supported structured outputs (https://platform.openai.com/docs/guides/structured-outputs).

Let me think about what the right move here. One option is to add a model setting to use JSON mode instead of structured output - but that's risky because there's no way to indicate what schema the JSON should use.

If you (or anyone in the community) has ideas - please share here (before creating a PR)

@rm-openai rm-openai added bug Something isn't working and removed question Question about using the SDK labels Mar 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants