Lazy creation of Teams ApiClient - #505
Closed
Rodrigo Brandão (rodrigobr-msft) wants to merge 1 commit into
Closed
Conversation
Copilot started reviewing on behalf of
Rodrigo Brandão (rodrigobr-msft)
July 28, 2026 16:30
View session
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the Microsoft Teams hosting layer so the microsoft_teams.api.ApiClient is no longer eagerly created during the Teams before_turn hook, and instead is created/cached on-demand when requested via TeamsTurnContext.api_client or TeamsAgentExtension.get_teams_api_client().
Changes:
- Remove eager Teams
ApiClientcreation from the Teamsbefore_turnhook and adjust tests accordingly. - Make
TeamsTurnContext.api_clientandTeamsAgentExtension.get_teams_api_client()lazily create/cache anApiClientwhen absent. - Simplify the Teams API client helper by removing
_get_teams_api_clientand making_set_teams_api_clientreturn the created/cached client.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| libraries/microsoft-agents-hosting-msteams/microsoft_agents/hosting/msteams/teams_turn_context.py | Switch api_client accessor to lazy-get/create behavior. |
| libraries/microsoft-agents-hosting-msteams/microsoft_agents/hosting/msteams/teams_agent_extension.py | Stop creating ApiClient in before_turn; lazily create in get_teams_api_client. |
| libraries/microsoft-agents-hosting-msteams/microsoft_agents/hosting/msteams/_teams_api_client.py | Remove _get_teams_api_client; make _set_teams_api_client return the client. |
| tests/hosting_msteams/test_teams_turn_context.py | Add a unit test for returning a cached Teams ApiClient. |
| tests/hosting_msteams/test_teams_agent_extension.py | Update hook test expectations after removing eager ApiClient creation. |
| tests/hosting_msteams/test_internal.py | Remove tests for deleted _get_teams_api_client helper. |
| tests/_common/testing_objects/mocks/mock_user_token_client.py | Reformat an AsyncMock assignment (no functional change). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+99
to
+102
| api_client = self._services.get(ApiClient) | ||
| if not api_client: | ||
| return _set_teams_api_client(self, self._app.connection_manager) | ||
| return api_client |
Comment on lines
+300
to
+303
| api_client = context.services.get(ApiClient) | ||
| if not api_client: | ||
| return _set_teams_api_client(context, self._app.connection_manager) | ||
| return api_client |
Comment on lines
23
to
28
| """ | ||
| Set the Teams API client in the context if it is not already set. | ||
|
|
||
| :param context: The turn context. | ||
| :param connection_manager: The connection manager. | ||
| """ |
Comment on lines
125
to
128
| assert result is True | ||
| assert isinstance(activity.channel_data, ChannelData) | ||
| assert activity.channel_data.channel.id == "c1" | ||
| assert ctx.services.has(ApiClient) | ||
|
|
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.
No description provided.