-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Python: fix(python): normalize Azure AI agent response_format #13891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| BaseAsyncAgentEventHandler, | ||
| FunctionToolDefinition, | ||
| RequiredMcpToolCall, | ||
| ResponseFormatJsonSchema, | ||
| ResponseFormatJsonSchemaType, | ||
| RunStep, | ||
| RunStepAzureAISearchToolCall, | ||
|
|
@@ -902,15 +903,47 @@ def _merge_options( | |
|
|
||
| Run-level parameters take precedence. | ||
| """ | ||
| normalized_response_format = ( | ||
| cls._normalize_response_format(response_format) | ||
| if response_format is not None | ||
| else cls._normalize_response_format(agent.definition.response_format) | ||
| ) | ||
| return { | ||
| "model": model if model is not None else agent.definition.model, | ||
| "response_format": response_format if response_format is not None else agent.definition.response_format, | ||
| "response_format": normalized_response_format, | ||
| "temperature": temperature if temperature is not None else None, | ||
| "top_p": top_p if top_p is not None else None, | ||
| "metadata": metadata if metadata is not None else agent.definition.metadata, | ||
| **kwargs, | ||
| } | ||
|
|
||
| @classmethod | ||
| def _normalize_response_format( | ||
| cls: type[_T], response_format: ResponseFormatJsonSchemaType | dict[str, Any] | None | ||
| ) -> ResponseFormatJsonSchemaType | dict[str, Any] | None: | ||
| """Normalize structured output response formats for Azure SDK consumers.""" | ||
| if response_format is None or isinstance(response_format, ResponseFormatJsonSchemaType): | ||
| return response_format | ||
|
|
||
| if not isinstance(response_format, dict): | ||
| return response_format | ||
|
|
||
| if response_format.get("type") != "json_schema": | ||
| return response_format | ||
|
|
||
| json_schema = response_format.get("json_schema") | ||
| if not isinstance(json_schema, dict): | ||
| return response_format | ||
|
|
||
|
Comment on lines
+928
to
+943
|
||
| return ResponseFormatJsonSchemaType( | ||
| json_schema=ResponseFormatJsonSchema( | ||
| name=json_schema.get("name"), | ||
| description=json_schema.get("description"), | ||
| schema=json_schema.get("schema"), | ||
| strict=json_schema.get("strict"), | ||
| ) | ||
| ) | ||
|
|
||
| @classmethod | ||
| def _generate_options(cls: type[_T], **kwargs: Any) -> dict[str, Any]: | ||
| """Generate a dictionary of options that can be passed directly to create_run.""" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_merge_options/_normalize_response_formatcurrently assumeresponse_formatisResponseFormatJsonSchemaType | dict | None, butAzureAIAgentpassesresponse_formatthrough asstr | ResponseFormatJsonSchemaType(seeAgentsApiResponseFormatOption), and this method already returns non-dict values unchanged. To keep type hints accurate (and avoid needingtype: ignore), consider widening the accepted/returned types here to includestr(and ideally align withAzureAIAgent.AgentsApiResponseFormatOption).