AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Get_from_dict_or_env From Langchain_core.Utils Has Incorrect Signature Or Incorrectly Casting Output

The utility function `get_from_dict_or_env` in `langchain_core.utils.env` accepts a dictionary with values of type `Any`, but internally casts the value to `str` before returning it. This breaks code that stores non-string values (e.g., enums, integers, custom objects) in the dictionary, as the returned value no longer matches the original type. The function signature declares `dict[str, Any]`, implying that the value type is preserved, but the implementation violates this contract.

highConfidence 87%LangChainChat ModelsEmbeddingsAffected V1.x

Origin Analysis

The function was modified (likely in PR #34204) to always return a string by applying `str()` to the dictionary value. This was done to normalize the return type to `str` for environment variables and defaults, but it ignored the fact that the input dictionary explicitly allows any value type. The root cause is a mismatch between the type annotation (`dict[str, Any]`) and the runtime coercion (`str(value)`), leading to silent type conversion and downstream failures for non-string dictionary entries.
```python from enum import Enum from langchain_core.utils import get_from_dict_or_env class Color(Enum): RED = "red" d = {"key": Color.RED} result = get_from_dict_or_env(d, "key", "KEY") print(type(result), result) # <class 'str'> 'Color.RED' instead of Color.RED assert result == Color.RED # Fails because str != Enum ```

Fixing Code Block

Edge Case Audit

Changing the return type from `str` to `Any` may break downstream code that assumes the result is always a string and calls string methods on it (e.g., `.lower()`, `.strip()`). In multi-threaded or concurrent scenarios, the function itself is stateless and thread-safe, but downstream consumers may rely on the old string type for hashing or serialization. On cross-platform systems, environment variables remain strings, but the default parameter can now be any type, which may surprise code expecting a string default. Rollback advice: pin `langchain-core` to the previous version or monkeypatch the function to restore the `str()` cast if string-only behavior is required. A safer alternative is to provide an optional `cast_type` parameter or separate functions for typed retrieval.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state