Skip to content

refactor(api): type _error_to_stream_response with ErrorStreamResponseDict TypedDict#34982

Open
aviu16 wants to merge 1 commit intolanggenius:mainfrom
aviu16:refactor/type-error-stream-response
Open

refactor(api): type _error_to_stream_response with ErrorStreamResponseDict TypedDict#34982
aviu16 wants to merge 1 commit intolanggenius:mainfrom
aviu16:refactor/type-error-stream-response

Conversation

@aviu16
Copy link
Copy Markdown
Contributor

@aviu16 aviu16 commented Apr 12, 2026

Part of #32863 (api/core/app/apps/base_app_generate_response_converter.py)

Summary

  • Define ErrorStreamResponseDict TypedDict for the _error_to_stream_response return type
  • Replace dict[str, Any] return annotation and data variable annotation with the new TypedDict

Why this change

_error_to_stream_response returns a fixed 3-key dict (code, message, status) for every code path. The bare dict[str, Any] hides this contract from the stream response serializer and callers in the chat/completion pipelines.

Changes

  • api/core/app/apps/base_app_generate_response_converter.py: Define ErrorStreamResponseDict, annotate _error_to_stream_response return type and data local variable

@aviu16 aviu16 requested a review from QuantumGhost as a code owner April 12, 2026 03:15
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. refactor labels Apr 12, 2026
@asukaminato0721 asukaminato0721 requested a review from Copilot April 12, 2026 03:35
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-04-12 03:37:02.855638893 +0000
+++ /tmp/pyrefly_pr.txt	2026-04-12 03:36:54.467662901 +0000
@@ -14,6 +14,20 @@
   --> controllers/console/setup.py:65:2
 ERROR Object of class `MissingRouter` has no attribute `get` [missing-attribute]
   --> controllers/console/version.py:30:2
+ERROR No matching overload found for function `typing.MutableMapping.update` called with arguments: (ErrorStreamResponseDict) [no-matching-overload]
+  --> core/app/apps/agent_chat/generate_response_converter.py:82:38
+ERROR No matching overload found for function `typing.MutableMapping.update` called with arguments: (ErrorStreamResponseDict) [no-matching-overload]
+   --> core/app/apps/agent_chat/generate_response_converter.py:118:38
+ERROR `dict[str, Any]` is not assignable to variable `data` with type `ErrorStreamResponseDict | None` [bad-assignment]
+   --> core/app/apps/base_app_generate_response_converter.py:139:24
+ERROR No matching overload found for function `typing.MutableMapping.update` called with arguments: (ErrorStreamResponseDict) [no-matching-overload]
+  --> core/app/apps/chat/generate_response_converter.py:82:38
+ERROR No matching overload found for function `typing.MutableMapping.update` called with arguments: (ErrorStreamResponseDict) [no-matching-overload]
+   --> core/app/apps/chat/generate_response_converter.py:118:38
+ERROR No matching overload found for function `typing.MutableMapping.update` called with arguments: (ErrorStreamResponseDict) [no-matching-overload]
+  --> core/app/apps/completion/generate_response_converter.py:80:38
+ERROR No matching overload found for function `typing.MutableMapping.update` called with arguments: (ErrorStreamResponseDict) [no-matching-overload]
+   --> core/app/apps/completion/generate_response_converter.py:117:38
 ERROR Class member `EasyUIBasedGenerateTaskPipeline._application_generate_entity` overrides parent class `BasedGenerateTaskPipeline` in an inconsistent manner [bad-override]
   --> core/app/task_pipeline/easy_ui_based_generate_task_pipeline.py:75:5
 ERROR `+=` is not supported between `list[PromptMessageContentUnionTypes]` and `str` [unsupported-operation]

Copy link
Copy Markdown
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 refactors _error_to_stream_response in the API app response conversion layer to make its return type explicit via a dedicated TypedDict, improving type safety for downstream stream response serialization/callers.

Changes:

  • Introduces ErrorStreamResponseDict TypedDict to describe the error stream payload shape.
  • Updates _error_to_stream_response return type (and a local variable) from dict[str, Any] to ErrorStreamResponseDict.
Comments suppressed due to low confidence (1)

api/core/app/apps/base_app_generate_response_converter.py:140

  • data is annotated as ErrorStreamResponseDict, but error_responses values are plain dict[str, Any] and several entries omit the required message key. With basedpyright, assigning v to data and later mutating it via setdefault will not type-check against a total TypedDict. Consider typing error_responses with a (partial) TypedDict (e.g., message as NotRequired) and constructing a new ErrorStreamResponseDict that always includes code, status, and a computed message before returning (instead of setdefault).
        data: ErrorStreamResponseDict | None = None
        for k, v in error_responses.items():
            if isinstance(e, k):
                data = v


💡 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

refactor size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants