Skip to content

Recursive filter_none in Inference Providers #3178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 25, 2025
Merged
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
36 changes: 32 additions & 4 deletions src/huggingface_hub/inference/_providers/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from huggingface_hub import constants
from huggingface_hub.hf_api import InferenceProviderMapping
from huggingface_hub.inference._common import RequestParameters
from huggingface_hub.inference._generated.types.chat_completion import ChatCompletionInputMessage
from huggingface_hub.utils import build_hf_headers, get_token, logging


Expand Down Expand Up @@ -36,8 +37,32 @@
}


def filter_none(d: Dict[str, Any]) -> Dict[str, Any]:
return {k: v for k, v in d.items() if v is not None}
def filter_none(obj: Union[Dict[str, Any], List[Any]]) -> Dict[str, Any]:
if isinstance(obj, dict):
cleaned: Dict[str, Any] = {}
for k, v in obj.items():
if v is None:
continue
if isinstance(v, (dict, list)):
v = filter_none(v)
# remove empty nested dicts
if isinstance(v, dict) and not v:
continue
cleaned[k] = v
return cleaned

if isinstance(obj, list):
cleaned_list: List[Any] = []
for v in obj:
if isinstance(v, (dict, list)):
v = filter_none(v)
if isinstance(v, dict) and not v:
continue

cleaned_list.append(v)
return cleaned_list # type: ignore [return-value]

raise ValueError(f"Expected dict or list, got {type(obj)}")


class TaskProviderHelper:
Expand Down Expand Up @@ -224,9 +249,12 @@ def _prepare_route(self, mapped_model: str, api_key: str) -> str:
return "/v1/chat/completions"

def _prepare_payload_as_dict(
self, inputs: Any, parameters: Dict, provider_mapping_info: InferenceProviderMapping
self,
inputs: List[Union[Dict, ChatCompletionInputMessage]],
parameters: Dict,
provider_mapping_info: InferenceProviderMapping,
) -> Optional[Dict]:
return {"messages": inputs, **filter_none(parameters), "model": provider_mapping_info.provider_id}
return filter_none({"messages": inputs, **parameters, "model": provider_mapping_info.provider_id})


class BaseTextGenerationTask(TaskProviderHelper):
Expand Down
2 changes: 1 addition & 1 deletion src/huggingface_hub/inference/_providers/hf_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def _prepare_payload_as_bytes(
provider_mapping_info: InferenceProviderMapping,
extra_payload: Optional[Dict],
) -> Optional[bytes]:
parameters = filter_none({k: v for k, v in parameters.items() if v is not None})
parameters = filter_none(parameters)
extra_payload = extra_payload or {}
has_parameters = len(parameters) > 0 or len(extra_payload) > 0

Expand Down
123 changes: 123 additions & 0 deletions tests/test_inference_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
BaseConversationalTask,
BaseTextGenerationTask,
TaskProviderHelper,
filter_none,
recursive_merge,
)
from huggingface_hub.inference._providers.black_forest_labs import BlackForestLabsTextToImageTask
Expand Down Expand Up @@ -1152,6 +1153,98 @@ def test_prepare_payload(self):
"model": "test-provider-id",
}

@pytest.mark.parametrize(
"raw_messages, expected_messages",
[
(
[
{
"role": "assistant",
"content": "",
"tool_calls": None,
}
],
[
{
"role": "assistant",
"content": "",
}
],
),
(
[
{
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_1",
"type": "function",
"function": {
"name": "get_current_weather",
"arguments": '{"location": "San Francisco, CA", "unit": "celsius"}',
},
},
],
},
{
"role": "tool",
"content": "pong",
"tool_call_id": "abc123",
"name": "dummy_tool",
"tool_calls": None,
},
],
[
{
"role": "assistant",
"tool_calls": [
{
"id": "call_1",
"type": "function",
"function": {
"name": "get_current_weather",
"arguments": '{"location": "San Francisco, CA", "unit": "celsius"}',
},
}
],
},
{
"role": "tool",
"content": "pong",
"tool_call_id": "abc123",
"name": "dummy_tool",
},
],
),
],
)
def test_prepare_payload_filters_messages(self, raw_messages, expected_messages):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for adding this :)

helper = BaseConversationalTask(provider="test-provider", base_url="https://api.test.com")

parameters = {
"temperature": 0.2,
"max_tokens": None,
"top_p": None,
}

payload = helper._prepare_payload_as_dict(
inputs=raw_messages,
parameters=parameters,
provider_mapping_info=InferenceProviderMapping(
provider="test-provider",
hf_model_id="test-model",
providerId="test-provider-id",
task="conversational",
status="live",
),
)

assert payload["messages"] == expected_messages
assert payload["temperature"] == 0.2
assert "max_tokens" not in payload
assert "top_p" not in payload


class TestBaseTextGenerationTask:
def test_prepare_route(self):
Expand Down Expand Up @@ -1236,6 +1329,36 @@ def test_recursive_merge(dict1: Dict, dict2: Dict, expected: Dict):
assert dict2 == initial_dict2


@pytest.mark.parametrize(
"data, expected",
[
({}, {}), # empty dictionary remains empty
({"a": 1, "b": None, "c": 3}, {"a": 1, "c": 3}), # remove None at root level
({"a": None, "b": {"x": None, "y": 2}}, {"b": {"y": 2}}), # remove nested None
({"a": {"b": {"c": None}}}, {}), # remove empty nested dict
(
{"a": "", "b": {"x": {"y": None}, "z": 0}, "c": []}, # do not remove 0, [] and "" values
{"a": "", "b": {"z": 0}, "c": []},
),
(
{"a": [0, 1, None]}, # do not remove None in lists
{"a": [0, 1, None]},
),
# dicts inside list are cleaned, list level None kept
({"a": [{"x": None, "y": 1}, None]}, {"a": [{"y": 1}, None]}),
# remove every None that is the value of a dict key
(
[None, {"x": None, "y": 5}, [None, 6]],
[None, {"y": 5}, [None, 6]],
),
({"a": [None, {"x": None}]}, {"a": [None]}),
],
)
def test_filter_none(data: Dict, expected: Dict):
"""Test that filter_none removes None values from nested dictionaries."""
assert filter_none(data) == expected


def test_get_provider_helper_auto(mocker):
"""Test the 'auto' provider selection logic."""

Expand Down