Skip to content

chore: add type hints to FastAPI SSE generator functions#5266

Open
Elbehery wants to merge 1 commit intollamastack:mainfrom
Elbehery:20260324_fix_type_annotations_agents
Open

chore: add type hints to FastAPI SSE generator functions#5266
Elbehery wants to merge 1 commit intollamastack:mainfrom
Elbehery:20260324_fix_type_annotations_agents

Conversation

@Elbehery
Copy link
Contributor

What does this PR do?

This change adds complete type annotations to sse_generator and
_preserve_context_for_sse functions in agents/fastapi_routes.py to satisfy
mypy's --disallow-untyped-defs requirement. These functions handle
Server-Sent Events streaming for the Agents API responses.

The sse_generator function converts async iterators to SSE format, while
_preserve_context_for_sse preserves request context across async tasks for
streaming responses. Type hints use AsyncIterator to properly type the
async generator functions.

A type ignore comment is used for the contextvars.Context.run pattern where
the type system has limitations in expressing the dynamic task creation. All
changes pass mypy strict checking and maintain full backward compatibility.

Test Plan

# 1. Run standard mypy check
  ./scripts/uv-run-with-index.sh run --group dev --group type_checking mypy \
    src/llama_stack_api/agents/fastapi_routes.py

  # 2. Run strict type checking (the key test)
  ./scripts/uv-run-with-index.sh run --group dev --group type_checking mypy --disallow-untyped-defs \
    src/llama_stack_api/agents/fastapi_routes.py

  # 3. Run all checks in one command
  ./scripts/uv-run-with-index.sh run --group dev --group type_checking mypy --disallow-untyped-defs \
    src/llama_stack_api/agents/fastapi_routes.py && \
  uv run ruff check \
    src/llama_stack_api/agents/fastapi_routes.py && \
  uv run black --check \
    src/llama_stack_api/agents/fastapi_routes.py && \
  ./scripts/uv-run-with-index.sh run --group dev --group type_checking mypy

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 24, 2026
@Elbehery
Copy link
Contributor Author

cc @leseb @skamenan7 @derekhiggins

@Elbehery Elbehery force-pushed the 20260324_fix_type_annotations_agents branch from fb92f59 to f4394c4 Compare March 24, 2026 15:21
Copy link
Collaborator

@cdoern cdoern left a comment

Choose a reason for hiding this comment

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

is there a way for this testing (rather than by hand) to be done by mypy going forward?

@Elbehery
Copy link
Contributor Author

is there a way for this testing (rather than by hand) to be done by mypy going forward?

I can add tests for this, historically I was just running the cmd :D

@Elbehery
Copy link
Contributor Author

is there a way for this testing (rather than by hand) to be done by mypy going forward?

I added #5269

we can update the files section as we progress

leseb
leseb previously requested changes Mar 24, 2026
Copy link
Collaborator

@leseb leseb left a comment

Choose a reason for hiding this comment

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

remove the pyproject.toml entry of the file you're fixing

@leseb
Copy link
Collaborator

leseb commented Mar 24, 2026

is there a way for this testing (rather than by hand) to be done by mypy going forward?

removing the ignore entry from pyproject is how we validate this with CI

EDIT: nevermind, was thinkig static type checking

@leseb leseb self-requested a review March 24, 2026 17:19
@leseb leseb dismissed their stale review March 24, 2026 17:19

was thinking static type checking

  This change adds complete type annotations to sse_generator and
  _preserve_context_for_sse functions in agents/fastapi_routes.py to satisfy
  mypy's --disallow-untyped-defs requirement. These functions handle
  Server-Sent Events streaming for the Agents API responses.

  The sse_generator function converts async iterators to SSE format, while
  _preserve_context_for_sse preserves request context across async tasks for
  streaming responses. Type hints use AsyncIterator to properly type the
  async generator functions.

  A type ignore comment is used for the contextvars.Context.run pattern where
  the type system has limitations in expressing the dynamic task creation. All
  changes pass mypy strict checking and maintain full backward compatibility.

  Signed-off-by: Mustafa Elbehery <melbeher@redhat.com>
@Elbehery Elbehery force-pushed the 20260324_fix_type_annotations_agents branch from 5f80415 to eb7c144 Compare March 25, 2026 08:56
@mergify
Copy link
Contributor

mergify bot commented Mar 26, 2026

This pull request has merge conflicts that must be resolved before it can be merged. @Elbehery please rebase it. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify bot added the needs-rebase label Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. needs-rebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants