Python: fix(python): Use AgentResponse.value instead of model_validate_json in HITL sample#4405
Merged
moonbox3 merged 2 commits intomicrosoft:mainfrom Mar 3, 2026
Conversation
…n 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 microsoft#4396
Contributor
There was a problem hiding this comment.
Pull request overview
Updates the Python HITL guessing game sample to use the framework’s structured-output accessor (AgentResponse.value) instead of manually parsing JSON with model_validate_json, aligning the sample with current structured output ergonomics.
Changes:
- Replaced manual JSON parsing (
GuessOutput.model_validate_json(...)) withresult.agent_response.value. - Updated inline documentation/comments to describe
.valueusage. - Removed the now-unneeded intermediate
textvariable.
python/samples/03-workflows/human-in-the-loop/guessing_game_with_human_input.py
Outdated
Show resolved
Hide resolved
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.
LEDazzio01
commented
Mar 2, 2026
Contributor
Author
LEDazzio01
left a comment
There was a problem hiding this comment.
Great catch! Pushed commit a76092d with the safety guard:
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.guessThis ensures a clear error message instead of an AttributeError if .value is None due to response_format not being propagated through the streaming path.
TaoChenOSU
approved these changes
Mar 2, 2026
moonbox3
approved these changes
Mar 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #4396
The HITL
guessing_game_with_human_input.pysample usedmodel_validate_json()to manually parse the agent's structured output:Since the agent is configured with
response_format=GuessOutput, theAgentResponseobject already provides a.valueproperty that returns the parsed Pydantic model directly:Changes
guessing_game_with_human_input.pyGuessOutput.model_validate_json(text).guess→result.agent_response.value.guess.valueusagetextvariable assignmentNotes
Other sample files (
edge_condition.py,multi_selection_edge_group.py) also usemodel_validate_jsonwith structured outputs, but their usage is in edge conditions and non-AgentExecutorResponsecontexts (e.g. parsing fromChatClient.get_response().messages[-1].text), which are more nuanced. This PR focuses on the primary file called out in the issue. A follow-up PR can address the remaining samples if desired.