langchain - 💡(How to fix) Fix Typing issue in StructuredTool._injected_args_keys [4 comments, 4 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
langchain-ai/langchain#36221Fetched 2026-04-08 01:26:19
View on GitHub
Comments
4
Participants
4
Timeline
9
Reactions
0
Author
Timeline (top)
commented ×4labeled ×3cross-referenced ×1issue_type_added ×1

There is a mypy typing issue in StructuredTool._injected_args_keys where the return type can be inferred as Any instead of frozenset[str].

This happens due to untyped constants affecting type inference.

Proposed fix ensures consistent typing using proper annotations and casting.

I have already opened a PR to fix this.

Error Message

from langchain_core.tools.structured import StructuredTool

Running mypy on StructuredTool shows:

error: Returning Any from function declared to return "frozenset[str]"

Root Cause

There is a mypy typing issue in StructuredTool._injected_args_keys where the return type can be inferred as Any instead of frozenset[str].

This happens due to untyped constants affecting type inference.

Proposed fix ensures consistent typing using proper annotations and casting.

I have already opened a PR to fix this.

Fix Action

Fix / Workaround

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Code Example

from langchain_core.tools.structured import StructuredTool

# Running mypy on StructuredTool shows:
# error: Returning Any from function declared to return "frozenset[str]"

---
RAW_BUFFERClick to expand / collapse

Checked other resources

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-openrouter
  • langchain-perplexity
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

Related Issues / PRs

Related PR: #36217

Reproduction Steps / Example Code (Python)

from langchain_core.tools.structured import StructuredTool

# Running mypy on StructuredTool shows:
# error: Returning Any from function declared to return "frozenset[str]"

Error Message and Stack Trace (if applicable)

Description

There is a mypy typing issue in StructuredTool._injected_args_keys where the return type can be inferred as Any instead of frozenset[str].

This happens due to untyped constants affecting type inference.

Proposed fix ensures consistent typing using proper annotations and casting.

I have already opened a PR to fix this.

System Info

LangChain version: latest (main branch) Python version: 3.10+ OS: Linux

extent analysis

Fix Plan

To fix the mypy typing issue in StructuredTool._injected_args_keys, follow these steps:

  • Update the return type annotation to explicitly specify frozenset[str].
  • Ensure all constants used in the function are properly typed to avoid inference of Any.

Example code changes:

from langchain_core.tools.structured import StructuredTool
from typing import frozenset

class StructuredTool:
    # ...

    def _injected_args_keys(self) -> frozenset[str]:
        # Explicitly type constants and variables
        typed_constant: frozenset[str] = frozenset({"key1", "key2"})
        # ...
        return typed_constant  # Return typed constant

Alternatively, use the cast function from the typing module to cast untyped constants to the desired type:

from typing import cast, frozenset

# ...

def _injected_args_keys(self) -> frozenset[str]:
    untyped_constant = {"key1", "key2"}
    typed_constant: frozenset[str] = cast(frozenset[str], frozenset(untyped_constant))
    # ...
    return typed_constant

Verification

Verify the fix by re-running mypy on the updated StructuredTool class. The error message should be resolved, indicating that the return type of _injected_args_keys is now correctly inferred as frozenset[str].

Extra Tips

  • Use explicit type annotations for function return types and variable declarations to ensure consistent typing.
  • Avoid using untyped constants, as they can affect type inference and lead to errors.
  • Use the cast function from the typing module to cast untyped constants to the desired type when necessary.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

langchain - 💡(How to fix) Fix Typing issue in StructuredTool._injected_args_keys [4 comments, 4 participants]