Skip to content

fix(core): remove 5 unnecessary type: ignore comments in ai.py#36968

Closed
Yvan Wang (BootstrapperSBL) wants to merge 1 commit intolangchain-ai:masterfrom
BootstrapperSBL:fix/ai-py-unnecessary-type-ignore
Closed

fix(core): remove 5 unnecessary type: ignore comments in ai.py#36968
Yvan Wang (BootstrapperSBL) wants to merge 1 commit intolangchain-ai:masterfrom
BootstrapperSBL:fix/ai-py-unnecessary-type-ignore

Conversation

@BootstrapperSBL
Copy link
Copy Markdown

Fixes #36931


Five # type: ignore comments in AIMessage.content_blocks, AIMessageChunk.init_tool_calls, and AIMessageChunk.init_server_tool_calls were silencing mypy rather than addressing what it was complaining about. Swap each one for a cast("dict[str, Any]", ...) placed at the site where narrowing is actually needed.

For the typeddict-item sites the source ToolCall TypedDict in messages.tool does not declare index or extras, even though they exist at runtime — casting the source tool_call to dict[str, Any] lets mypy read those keys without silencing the diagnostic. For the three index sites, self.content[idx] is typed str | dict[str, Any] and mypy cannot narrow it from the outer isinstance(block, dict) check on a separate loop variable — casting the target to dict[str, Any] at the assignment point expresses the intent directly.

Runtime behavior is identical. The two load-bearing ignores called out in the issue (the type field override on AIMessageChunk and the __add__ overload) are left in place — they encode real architectural choices, not suppressed bugs.

Before:

tool_call_block["index"] = tool_call["index"]  # type: ignore[typeddict-item]

After:

tool_call_block["index"] = cast("dict[str, Any]", tool_call)["index"]

mypy langchain_core and mypy tests both still come back clean, and the full libs/core unit test suite passes locally.

Disclosure: this contribution was prepared with assistance from an AI coding agent; a human reviewed the change before opening the PR.

Swap five silencing `# type: ignore` comments in `AIMessage` and
`AIMessageChunk` for `cast("dict[str, Any]", ...)` at the point
where mypy needs the narrowing. Runtime semantics are unchanged; the
two intentional ignores on the `AIMessageChunk.type` assignment and
the `__add__` overload remain in place.
@github-actions github-actions Bot added core `langchain-core` package issues & PRs fix For PRs that implement a fix size: XS < 50 LOC labels Apr 23, 2026
@github-actions
Copy link
Copy Markdown

This PR has been automatically closed because you are not assigned to the linked issue.

External contributors must be assigned to an issue before opening a PR for it. Please:

  1. Comment on the linked issue to request assignment from a maintainer
  2. Once assigned, your PR will be reopened automatically

Maintainers: reopen this PR or remove the missing-issue-link label to bypass this check.

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

Labels

core `langchain-core` package issues & PRs external fix For PRs that implement a fix missing-issue-link new-contributor size: XS < 50 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(core): remove 5 unnecessary type: ignore comments in ai.py

1 participant