Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions libs/core/langchain_core/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ def get_from_dict_or_env(
"""
if isinstance(key, (list, tuple)):
for k in key:
if value := data.get(k):
return value
if k in data and data[k] is not None:
return data[k]

if isinstance(key, str) and key in data and data[key]:
if isinstance(key, str) and key in data and data[key] is not None:
return data[key]

key_for_err = key[0] if isinstance(key, (list, tuple)) else key
Expand Down
28 changes: 28 additions & 0 deletions libs/core/tests/unit_tests/utils/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,31 @@ def test_get_from_dict_or_env() -> None:
)
is None
)


def test_get_from_dict_or_env_empty_string() -> None:
"""Test that empty strings are returned consistently for list and string keys."""
data = {"key": "", "other": "value"}

# Test single string key with empty string value
assert get_from_dict_or_env(data, "key", "ENV_KEY", default="default") == ""

# Test list of keys with empty string value (should behave the same)
assert get_from_dict_or_env(data, ["key"], "ENV_KEY", default="default") == ""

# Test list of keys where first key has empty string
result = get_from_dict_or_env(data, ["key", "other"], "ENV_KEY", default="default")
assert result == ""


def test_get_from_dict_or_env_none_value() -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test passes on master

"""Test that None values are skipped and default is used."""
data = {"key": None, "other": "value"}

# Test single string key with None value - should use default
result = get_from_dict_or_env(data, "key", "ENV_KEY", default="default")
assert result == "default"

# Test list of keys with None value - should skip to next key
result = get_from_dict_or_env(data, ["key", "other"], "ENV_KEY", default="default")
assert result == "value"