Description
When I use a support
response_format = {
'type': ' json_object'
}The model is in
When conducting agent testing, the official demo did not run smoothly. Error, please refer to the code below:
The system used: macos
Partial software versions:
openai 1.68.0
openai-agents 0.0.6
pycparser 2.22
pydantic 2.10.6
my code :
`
-- coding: utf-8 --
@author : ysl
@file : main_bata.py
from agents import Agent, InputGuardrail,GuardrailFunctionOutput, Runner, OpenAIChatCompletionsModel
from pydantic import BaseModel
import asyncio
import os
from config.config import DOUBAO, ZHiPU
from openai import AsyncOpenAI
from agents import set_tracing_disabled
BASE_URL = ZHiPU.get("base_url") or ""
API_KEY = ZHiPU.get("api_key") or ""
MODEL_NAME = ZHiPU.get("model_name") or ""
if not BASE_URL or not API_KEY or not MODEL_NAME:
raise ValueError(
"Please set EXAMPLE_BASE_URL, EXAMPLE_API_KEY, EXAMPLE_MODEL_NAME via env var or code."
)
client = AsyncOpenAI(base_url=BASE_URL, api_key=API_KEY)
set_tracing_disabled(disabled=True)
class HomeworkOutput(BaseModel):
is_homework: bool
reasoning: str
guardrail_agent = Agent(
name="Guardrail check",
instructions="Check if the user is asking about homework.",
output_type=HomeworkOutput,
model=OpenAIChatCompletionsModel(
model=MODEL_NAME,
openai_client=client,
) # 使用豆包 LLM
)
math_tutor_agent = Agent(
name="Math Tutor",
handoff_description="Specialist agent for math questions",
instructions="You provide help with math problems. Explain your reasoning at each step and include examples",
model=OpenAIChatCompletionsModel(model=MODEL_NAME, openai_client=client) # 使用豆包 LLM
)
history_tutor_agent = Agent(
name="History Tutor",
handoff_description="Specialist agent for historical questions",
instructions="You provide assistance with historical queries. Explain important events and context clearly.",
model=OpenAIChatCompletionsModel(model=MODEL_NAME, openai_client=client) # 使用豆包 LLM
)
async def homework_guardrail(ctx, agent, input_data):
result = await Runner.run(guardrail_agent, input_data, context=ctx.context)
final_output = result.final_output_as(HomeworkOutput)
return GuardrailFunctionOutput(
output_info=final_output,
tripwire_triggered=not final_output.is_homework,
)
triage_agent = Agent(
name="Triage Agent",
instructions="You determine which agent to use based on the user's homework question",
handoffs=[history_tutor_agent, math_tutor_agent],
input_guardrails=[
InputGuardrail(guardrail_function=homework_guardrail),
],
model=OpenAIChatCompletionsModel(model=MODEL_NAME, openai_client=client) # 使用豆包 LLM
)
async def main():
result = await Runner.run(triage_agent, "who was the first president of the united states?")
print(result.final_output)
result = await Runner.run(triage_agent, "what is life")
print(result.final_output)
if name == "main":
asyncio.run(main())
`
Error:
`Traceback (most recent call last):
File "/Users/ysl/Downloads/works/workstation/mental_health_agent/.venv/lib/python3.12/site-packages/agents/util/_json.py", line 20, in validate_json
validated = type_adapter.validate_json(json_str, experimental_allow_partial=partial_setting)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ysl/Downloads/works/workstation/mental_health_agent/.venv/lib/python3.12/site-packages/pydantic/type_adapter.py", line 446, in validate_json
return self.validator.validate_json(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for HomeworkOutput
Invalid JSON: expected value at line 1 column 1 [type=json_invalid, input_value='The user is not asking a...t of the United States.', input_type=str]
For further information visit https://errors.pydantic.dev/2.10/v/json_invalid
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/ysl/Downloads/works/workstation/mental_health_agent/main_bata.py", line 80, in
asyncio.run(main())
File "/usr/local/Cellar/python@3.12/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/runners.py", line 194, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "/usr/local/Cellar/python@3.12/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/Cellar/python@3.12/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/base_events.py", line 686, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "/Users/ysl/Downloads/works/workstation/mental_health_agent/main_bata.py", line 73, in main
result = await Runner.run(triage_agent, "who was the first president of the united states?")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ysl/Downloads/works/workstation/mental_health_agent/.venv/lib/python3.12/site-packages/agents/run.py", line 210, in run
input_guardrail_results, turn_result = await asyncio.gather(
^^^^^^^^^^^^^^^^^^^^^
File "/Users/ysl/Downloads/works/workstation/mental_health_agent/.venv/lib/python3.12/site-packages/agents/run.py", line 794, in _run_input_guardrails
result = await done
^^^^^^^^^^
File "/usr/local/Cellar/python@3.12/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/tasks.py", line 631, in _wait_for_one
return f.result() # May raise f.exception().
^^^^^^^^^^
File "/Users/ysl/Downloads/works/workstation/mental_health_agent/.venv/lib/python3.12/site-packages/agents/_run_impl.py", line 645, in run_single_input_guardrail
result = await guardrail.run(agent, input, context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ysl/Downloads/works/workstation/mental_health_agent/.venv/lib/python3.12/site-packages/agents/guardrail.py", line 118, in run
output=await output,
^^^^^^^^^^^^
File "/Users/ysl/Downloads/works/workstation/mental_health_agent/main_bata.py", line 55, in homework_guardrail
result = await Runner.run(guardrail_agent, input_data, context=ctx.context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ysl/Downloads/works/workstation/mental_health_agent/.venv/lib/python3.12/site-packages/agents/run.py", line 210, in run
input_guardrail_results, turn_result = await asyncio.gather(
^^^^^^^^^^^^^^^^^^^^^
File "/Users/ysl/Downloads/works/workstation/mental_health_agent/.venv/lib/python3.12/site-packages/agents/run.py", line 729, in _run_single_turn
return await cls._get_single_step_result_from_response(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ysl/Downloads/works/workstation/mental_health_agent/.venv/lib/python3.12/site-packages/agents/run.py", line 761, in _get_single_step_result_from_response
return await RunImpl.execute_tools_and_side_effects(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ysl/Downloads/works/workstation/mental_health_agent/.venv/lib/python3.12/site-packages/agents/_run_impl.py", line 265, in execute_tools_and_side_effects
final_output = output_schema.validate_json(potential_final_output_text)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ysl/Downloads/works/workstation/mental_health_agent/.venv/lib/python3.12/site-packages/agents/agent_output.py", line 91, in validate_json
validated = _json.validate_json(json_str, self._type_adapter, partial)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ysl/Downloads/works/workstation/mental_health_agent/.venv/lib/python3.12/site-packages/agents/util/_json.py", line 29, in validate_json
raise ModelBehaviorError(
agents.exceptions.ModelBehaviorError: Invalid JSON when parsing The user is not asking about homework. They are asking a factual question about American history, specifically inquiring about the first president of the United States. for TypeAdapter(HomeworkOutput); 1 validation error for HomeworkOutput
Invalid JSON: expected value at line 1 column 1 [type=json_invalid, input_value='The user is not asking a...t of the United States.', input_type=str]
For further information visit https://errors.pydantic.dev/2.10/v/json_invalid
`