-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Python: Fix: Parse oauth_consent_request events in Azure AI client #4197
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
Open
giles17
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
giles17:oauth_event_fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+221
−8
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,9 @@ | |
| BaseContextProvider, | ||
| ChatAndFunctionMiddlewareTypes, | ||
| ChatMiddlewareLayer, | ||
| ChatResponse, | ||
| ChatResponseUpdate, | ||
| Content, | ||
| FunctionInvocationConfiguration, | ||
| FunctionInvocationLayer, | ||
| FunctionTool, | ||
|
|
@@ -579,6 +582,61 @@ def _get_current_conversation_id(self, options: Mapping[str, Any], **kwargs: Any | |
| """Get the current conversation ID from chat options or kwargs.""" | ||
| return options.get("conversation_id") or kwargs.get("conversation_id") or self.conversation_id | ||
|
|
||
| @override | ||
| def _parse_response_from_openai( | ||
| self, | ||
| response: Any, | ||
| options: dict[str, Any], | ||
| ) -> ChatResponse: | ||
| """Parse an Azure AI Responses API response, handling Azure-specific output item types.""" | ||
| result = super()._parse_response_from_openai(response, options) | ||
|
|
||
| for item in response.output: | ||
| if item.type == "oauth_consent_request": | ||
| consent_link = item.consent_link | ||
| if consent_link and result.messages: | ||
| result.messages[0].contents.append( | ||
| Content.from_oauth_consent_request( | ||
| consent_link=consent_link, | ||
| raw_representation=item, | ||
| ) | ||
| ) | ||
| elif not consent_link: | ||
| logger.warning("Received oauth_consent_request output without consent_link: %s", item) | ||
|
|
||
| return result | ||
|
|
||
| @override | ||
| def _parse_chunk_from_openai( | ||
| self, | ||
| event: Any, | ||
| options: dict[str, Any], | ||
| function_call_ids: dict[int, tuple[str, str]], | ||
| ) -> ChatResponseUpdate: | ||
| """Parse an Azure AI streaming event, handling Azure-specific event types.""" | ||
| # Intercept output_item.added events for Azure-specific item types | ||
| if event.type == "response.output_item.added" and event.item.type == "oauth_consent_request": | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and this logic will silently ignore other content types? And that seems inconsistent with the non-streaming flow? |
||
| event_item = event.item | ||
| consent_link = event_item.consent_link | ||
| contents: list[Content] = [] | ||
| if consent_link: | ||
| contents.append( | ||
| Content.from_oauth_consent_request( | ||
| consent_link=consent_link, | ||
| raw_representation=event_item, | ||
| ) | ||
| ) | ||
| else: | ||
| logger.warning("Received oauth_consent_request output without consent_link: %s", event_item) | ||
| return ChatResponseUpdate( | ||
| contents=contents, | ||
| role="assistant", | ||
| model_id=self.model_id, | ||
| raw_representation=event, | ||
| ) | ||
|
|
||
| return super()._parse_chunk_from_openai(event, options, function_call_ids) | ||
|
|
||
| def _prepare_messages_for_azure_ai(self, messages: Sequence[Message]) -> tuple[list[Message], str | None]: | ||
| """Prepare input from messages and convert system/developer messages to instructions.""" | ||
| result: list[Message] = [] | ||
|
|
||
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
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
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
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
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
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.
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.
this logic will mean it drops silently if the
result.messageslist is empty, probably not intended?