From ef8e18fb85ef06f3c8cd0c95e77f57373acdd672 Mon Sep 17 00:00:00 2001 From: "L. Elaine Dazzio" Date: Mon, 2 Mar 2026 22:06:08 -0500 Subject: [PATCH] Python: fix(python): Use AgentResponse.value instead of model_validate_json in HITL sample (#4405) * fix(python): use AgentResponse.value instead of model_validate_json in HITL sample Since the agent is configured with response_format=GuessOutput, the AgentResponse already provides .value with the parsed Pydantic model. Using .value is more idiomatic and avoids redundant JSON parsing. Fixes #4396 * fix: add safety guard for AgentResponse.value being None Address Copilot review feedback: .value is optional and may be None if response_format isn't propagated through the streaming path. Add an explicit None check with a clear error message. --- .../guessing_game_with_human_input.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/python/samples/03-workflows/human-in-the-loop/guessing_game_with_human_input.py b/python/samples/03-workflows/human-in-the-loop/guessing_game_with_human_input.py index 06e9a738f5..f764de6cb7 100644 --- a/python/samples/03-workflows/human-in-the-loop/guessing_game_with_human_input.py +++ b/python/samples/03-workflows/human-in-the-loop/guessing_game_with_human_input.py @@ -102,12 +102,19 @@ class TurnManager(Executor): """Handle the agent's guess and request human guidance. Steps: - 1) Parse the agent's JSON into GuessOutput for robustness. + 1) Use .value to access the parsed structured output directly. 2) Request info with a HumanFeedbackRequest as the payload. """ - # Parse structured model output - text = result.agent_response.text - last_guess = GuessOutput.model_validate_json(text).guess + # Access the parsed structured model output via .value. + # Since the agent is configured with response_format=GuessOutput, + # .value returns the parsed GuessOutput instance directly. + agent_value = result.agent_response.value + if agent_value is None: + raise RuntimeError( + "AgentResponse.value is None. Ensure that the agent is invoked with " + "options={'response_format': GuessOutput} so structured output is available." + ) + last_guess = agent_value.guess # Craft a precise human prompt that defines higher and lower relative to the agent's guess. prompt = (