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 1 commit
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
35 changes: 27 additions & 8 deletions src/huggingface_hub/inference/_providers/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,32 @@
}


def filter_none(d: Dict[str, Any]) -> Dict[str, Any]:
return {
k: v_filtered
for k, v in d.items()
if (v_filtered := filter_none(v) if isinstance(v, dict) else v) is not None and v_filtered != {}
}
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 @@ -234,8 +254,7 @@ def _prepare_payload_as_dict(
parameters: Dict,
provider_mapping_info: InferenceProviderMapping,
) -> Optional[Dict]:
inputs = [filter_none(message) for message in inputs]
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
100 changes: 100 additions & 0 deletions tests/test_inference_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1153,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 @@ -1252,6 +1344,14 @@ def test_recursive_merge(dict1: Dict, dict2: Dict, expected: Dict):
{"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):
Expand Down