Python: [BREAKING] Make response_format validation errors visible to users (#3274)

* Make response_format validation errors visible to users

* Small fix

* Addressed comments
This commit is contained in:
Dmytro Struk
2026-01-19 09:20:46 -08:00
committed by GitHub
Unverified
parent 3c1be2a713
commit 83e8965c8e
12 changed files with 298 additions and 62 deletions
@@ -56,13 +56,14 @@ async def main() -> None:
result1 = await agent.run(query1)
if isinstance(result1.value, WeatherInfo):
weather = result1.value
if weather := result1.try_parse_value(WeatherInfo):
print("Agent:")
print(f" Location: {weather.location}")
print(f" Temperature: {weather.temperature}")
print(f" Conditions: {weather.conditions}")
print(f" Recommendation: {weather.recommendation}")
else:
print(f"Failed to parse response: {result1.text}")
# Request 2: Override response_format at runtime with CityInfo
print("\n--- Request 2: Runtime override with CityInfo ---")
@@ -71,12 +72,13 @@ async def main() -> None:
result2 = await agent.run(query2, options={"response_format": CityInfo})
if isinstance(result2.value, CityInfo):
city = result2.value
if city := result2.try_parse_value(CityInfo):
print("Agent:")
print(f" City: {city.city_name}")
print(f" Population: {city.population}")
print(f" Country: {city.country}")
else:
print(f"Failed to parse response: {result2.text}")
if __name__ == "__main__":