langchain - ✅(Solved) Fix get_from_dict_or_env from langchain_core.utils has incorrect signature or incorrectly casting output [1 pull requests, 4 comments, 3 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#35285Fetched 2026-04-08 00:26:49
View on GitHub
Comments
4
Participants
3
Timeline
27
Reactions
0
Author
Assignees
Timeline (top)
cross-referenced ×6mentioned ×5subscribed ×5commented ×3

The str() cast being done on the output here: https://github.com/langchain-ai/langchain/blob/master/libs/core/langchain_core/utils/env.py#L53 is bad. This started breaking our code when I did the upgrade. So we have a enums as the dict values and this str casting is creating problems. While I was able to fix it on my end, I see some inconsistencies here:

The input dict has the signature as dict[str, Any]. If we are accepting Any, we should also return Any. But I see the default value and even the env variable would be str. So it might make sense to keep the return value type as str. But then we should change the signature of the input as dict[str, str] to prevent incorrect usage. I personally am okay with just changing the return type as Any and returning the original value instead of casting as str.

@cbornet, I see you also had a similar opinion when you made the change in this PR: https://github.com/langchain-ai/langchain/pull/34204. Can we properly fix this? Let me know if you want me to raise a PR

Error Message

Error Message and Stack Trace (if applicable)

Root Cause

The str() cast being done on the output here: https://github.com/langchain-ai/langchain/blob/master/libs/core/langchain_core/utils/env.py#L53 is bad. This started breaking our code when I did the upgrade. So we have a enums as the dict values and this str casting is creating problems. While I was able to fix it on my end, I see some inconsistencies here:

The input dict has the signature as dict[str, Any]. If we are accepting Any, we should also return Any. But I see the default value and even the env variable would be str. So it might make sense to keep the return value type as str. But then we should change the signature of the input as dict[str, str] to prevent incorrect usage. I personally am okay with just changing the return type as Any and returning the original value instead of casting as str.

@cbornet, I see you also had a similar opinion when you made the change in this PR: https://github.com/langchain-ai/langchain/pull/34204. Can we properly fix this? Let me know if you want me to raise a PR

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.

PR fix notes

PR #34204: style(core): fix mypy no-any-return violations

Description (problem / solution / changelog)

  • FIxed where possible
  • Used cast when not possible to fix

Changed files

  • libs/core/langchain_core/_api/internal.py (modified, +2/-1)
  • libs/core/langchain_core/language_models/base.py (modified, +2/-1)
  • libs/core/langchain_core/language_models/chat_models.py (modified, +5/-2)
  • libs/core/langchain_core/language_models/llms.py (modified, +4/-1)
  • libs/core/langchain_core/messages/base.py (modified, +1/-1)
  • libs/core/langchain_core/messages/utils.py (modified, +50/-12)
  • libs/core/langchain_core/output_parsers/base.py (modified, +3/-2)
  • libs/core/langchain_core/output_parsers/pydantic.py (modified, +12/-2)
  • libs/core/langchain_core/prompts/base.py (modified, +11/-11)
  • libs/core/langchain_core/prompts/dict.py (modified, +7/-4)
  • libs/core/langchain_core/prompts/few_shot_with_templates.py (modified, +2/-1)
  • libs/core/langchain_core/prompts/image.py (modified, +2/-2)
  • libs/core/langchain_core/prompts/message.py (modified, +1/-1)
  • libs/core/langchain_core/prompts/string.py (modified, +18/-10)
  • libs/core/langchain_core/runnables/base.py (modified, +4/-4)
  • libs/core/langchain_core/runnables/graph_mermaid.py (modified, +2/-2)
  • libs/core/langchain_core/runnables/graph_png.py (modified, +2/-2)
  • libs/core/langchain_core/runnables/router.py (modified, +2/-2)
  • libs/core/langchain_core/runnables/utils.py (modified, +5/-2)
  • libs/core/langchain_core/tools/base.py (modified, +2/-2)
  • libs/core/langchain_core/tools/convert.py (modified, +2/-2)
  • libs/core/langchain_core/tracers/context.py (modified, +1/-1)
  • libs/core/langchain_core/tracers/event_stream.py (modified, +2/-2)
  • libs/core/langchain_core/tracers/log_stream.py (modified, +2/-2)
  • libs/core/langchain_core/utils/env.py (modified, +2/-2)
  • libs/core/langchain_core/utils/function_calling.py (modified, +5/-3)
  • libs/core/langchain_core/utils/json.py (modified, +2/-2)
  • libs/core/langchain_core/utils/json_schema.py (modified, +4/-2)
  • libs/core/langchain_core/utils/pydantic.py (modified, +13/-12)
  • libs/core/langchain_core/vectorstores/utils.py (modified, +2/-2)
  • libs/core/pyproject.toml (modified, +0/-1)
  • libs/core/tests/unit_tests/messages/test_utils.py (modified, +2/-2)
  • libs/core/tests/unit_tests/prompts/test_structured.py (modified, +1/-2)
  • libs/core/tests/unit_tests/pydantic_utils.py (modified, +23/-22)
  • libs/core/tests/unit_tests/runnables/test_history.py (modified, +3/-2)
  • libs/core/tests/unit_tests/runnables/test_runnable.py (modified, +2/-2)
  • libs/core/tests/unit_tests/runnables/test_runnable_events_v1.py (modified, +2/-2)
  • libs/core/tests/unit_tests/runnables/test_runnable_events_v2.py (modified, +2/-2)
  • libs/core/tests/unit_tests/runnables/test_tracing_interops.py (modified, +10/-4)
  • libs/core/tests/unit_tests/runnables/test_utils.py (modified, +4/-4)
  • libs/core/tests/unit_tests/test_tools.py (modified, +7/-7)
  • libs/langchain/langchain_classic/agents/output_parsers/json.py (modified, +2/-2)

Code Example

from langchain_core.utils import get_from_dict_or_env

get_from_dict_or_env(enum_dict, "key")

---
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

No response

Reproduction Steps / Example Code (Python)

from langchain_core.utils import get_from_dict_or_env

get_from_dict_or_env(enum_dict, "key")

Error Message and Stack Trace (if applicable)

Description

The str() cast being done on the output here: https://github.com/langchain-ai/langchain/blob/master/libs/core/langchain_core/utils/env.py#L53 is bad. This started breaking our code when I did the upgrade. So we have a enums as the dict values and this str casting is creating problems. While I was able to fix it on my end, I see some inconsistencies here:

The input dict has the signature as dict[str, Any]. If we are accepting Any, we should also return Any. But I see the default value and even the env variable would be str. So it might make sense to keep the return value type as str. But then we should change the signature of the input as dict[str, str] to prevent incorrect usage. I personally am okay with just changing the return type as Any and returning the original value instead of casting as str.

@cbornet, I see you also had a similar opinion when you made the change in this PR: https://github.com/langchain-ai/langchain/pull/34204. Can we properly fix this? Let me know if you want me to raise a PR

System Info

langchain == 1.x stack. Can't share the entire text due to security concerns

extent analysis

Fix Plan

Step 1: Update get_from_dict_or_env function to return the original value

In the env.py file, update the get_from_dict_or_env function to return the original value instead of casting it to a string.

def get_from_dict_or_env(enum_dict, key):
    """
    Retrieves the value from the dictionary or environment variable.
    
    Args:
        enum_dict (dict): Dictionary containing enum values.
        key (str): Key to retrieve the value for.
    
    Returns:
        Any: The value associated with the key in the dictionary or environment variable.
    """
    return enum_dict.get(key) or os.environ.get(key)

Step 2: Update the function signature to reflect the return type

Update the function signature to reflect that it returns Any instead of str.

def get_from_dict_or_env(enum_dict: dict[str, Any], key: str) -> Any:
    ...

Step 3: Test the updated function

Test the updated function with a dictionary containing enum values and an environment variable.

enum_dict = {"key": "value"}
print(get_from_dict_or_env(enum_dict, "key"))  # Output: "value"
print(get_from_dict_or_env({"key": 123}, "key"))  # Output: 123
print(get_from_dict_or_env(os.environ, "KEY"))  # Output: value from environment variable

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 - ✅(Solved) Fix get_from_dict_or_env from langchain_core.utils has incorrect signature or incorrectly casting output [1 pull requests, 4 comments, 3 participants]