Skip to content

Python: fix(python): Use AgentResponse.value instead of model_validate_json in HITL sample#4405

Merged
moonbox3 merged 2 commits intomicrosoft:mainfrom
LEDazzio01:fix/4396-samples-use-response-value
Mar 3, 2026
Merged

Python: fix(python): Use AgentResponse.value instead of model_validate_json in HITL sample#4405
moonbox3 merged 2 commits intomicrosoft:mainfrom
LEDazzio01:fix/4396-samples-use-response-value

Conversation

@LEDazzio01
Copy link
Contributor

Summary

Fixes #4396

The HITL guessing_game_with_human_input.py sample used model_validate_json() to manually parse the agent's structured output:

text = result.agent_response.text
last_guess = GuessOutput.model_validate_json(text).guess

Since the agent is configured with response_format=GuessOutput, the AgentResponse object already provides a .value property that returns the parsed Pydantic model directly:

last_guess = result.agent_response.value.guess

Changes

guessing_game_with_human_input.py

  • Replaced GuessOutput.model_validate_json(text).guessresult.agent_response.value.guess
  • Updated docstring comments to explain .value usage
  • Removed unused text variable assignment

Notes

Other sample files (edge_condition.py, multi_selection_edge_group.py) also use model_validate_json with structured outputs, but their usage is in edge conditions and non-AgentExecutorResponse contexts (e.g. parsing from ChatClient.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.

…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
Copilot AI review requested due to automatic review settings March 2, 2026 22:45
@github-actions github-actions bot changed the title fix(python): Use AgentResponse.value instead of model_validate_json in HITL sample Python: fix(python): Use AgentResponse.value instead of model_validate_json in HITL sample Mar 2, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(...)) with result.agent_response.value.
  • Updated inline documentation/comments to describe .value usage.
  • Removed the now-unneeded intermediate text variable.

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.
Copy link
Contributor Author

@LEDazzio01 LEDazzio01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.guess

This ensures a clear error message instead of an AttributeError if .value is None due to response_format not being propagated through the streaming path.

@moonbox3 moonbox3 added this pull request to the merge queue Mar 3, 2026
Merged via the queue into microsoft:main with commit ef8e18f Mar 3, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python HITL docs/samples: use AgentResponse.value instead of model_validate_json for structured outputs

5 participants