Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Feb 1, 2026

FastAPI JWT middleware was incorrectly awaiting get_anonymous_claims(), a synchronous method, causing runtime errors in anonymous mode.

Changes

Fixed middleware (jwt_authorization_middleware.py line 65)

# Before
request.state.claims_identity = await token_validator.get_anonymous_claims()

# After  
request.state.claims_identity = token_validator.get_anonymous_claims()

The method signature in JwtTokenValidator is:

def get_anonymous_claims(self) -> ClaimsIdentity:  # Not async
    return ClaimsIdentity({}, False, authentication_type="Anonymous")

This aligns with the aiohttp middleware implementation which correctly calls it synchronously.

Original prompt

This section details on the original issue you should resolve

<issue_title>[FASTAPI] Object ClaimsIdentity can't be used in 'await' expression</issue_title>
<issue_description>When using Agents Playground in Anonymous mode the JWT middleware for fast API throws as its attempting to call a sync method via an Async pattern.

https://github.com/microsoft/Agents-for-python/blob/a54797cbbe267e0ea8c8b6f10b379a8918dc2d4e/libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/jwt_authorization_middleware.py#L66C7-L66C65</issue_description>

<agent_instructions>Create a fix for this issue to remove the await from the JWT Authorization Middleware for Fast API when creating a Claims identity.

Also write a test for this to add to the test suite. </agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits February 1, 2026 01:18
Co-authored-by: MattB-msft <10568244+MattB-msft@users.noreply.github.com>
Co-authored-by: MattB-msft <10568244+MattB-msft@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix await expression issue in JWT middleware for FastAPI Fix: Remove await from synchronous get_anonymous_claims() in FastAPI JWT middleware Feb 1, 2026
Copilot AI requested a review from MattB-msft February 1, 2026 01:21
@MattB-msft
Copy link
Member

@copilot run the python command black on the folder containing any files you modify, and accept those changes.

Copy link
Contributor Author

Copilot AI commented Feb 1, 2026

@copilot run the python command black on the folder containing any files you modify, and accept those changes.

Black has been run on both libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/ and tests/hosting_fastapi/ folders. All files are already properly formatted (no changes needed). The formatting was applied in commit 3439340.

@MattB-msft
Copy link
Member

@copilot remove the tests you added

Co-authored-by: MattB-msft <10568244+MattB-msft@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 1, 2026

@copilot remove the tests you added

Test files removed in commit 7372c87. The PR now contains only the middleware fix.

@MattB-msft
Copy link
Member

@copilot Add Tests back to this PR, Add the tests to the test_samples/fastapi folder in the project.

@MattB-msft MattB-msft marked this pull request as ready for review February 3, 2026 01:23
@MattB-msft MattB-msft requested a review from a team as a code owner February 3, 2026 01:23
Copilot AI review requested due to automatic review settings February 3, 2026 01:23
@MattB-msft MattB-msft merged commit f026337 into main Feb 3, 2026
14 of 15 checks passed
@MattB-msft MattB-msft deleted the copilot/fix-jwt-middleware-await-issue branch February 3, 2026 01:23
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

This PR fixes a runtime error in the FastAPI JWT authorization middleware by correctly treating get_anonymous_claims() as a synchronous method, aligning the behavior with the aiohttp middleware.

Changes:

  • Updated JwtAuthorizationMiddleware to call JwtTokenValidator.get_anonymous_claims() without await when handling anonymous access (no Authorization header and no CLIENT_ID configured).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FASTAPI] Object ClaimsIdentity can't be used in 'await' expression

3 participants