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

Conversation

Wauplin
Copy link
Contributor

@Wauplin Wauplin commented Jun 24, 2025

None values in payload can lead to a BadRequest error if the Inference Provider did not expect the field and is doing input validation. This is for instance what's happening in #3170 with groq complaining if tool_calls and tool_call_id are both set (with None values in them). Groq is complying to the OpenAI specs, this is more a problem on our side but won't be in this PR (*).

What we currently do to avoid these issues is to filter out None values using filter_none. However this helper only removes None values at the top-level of the dictionary. This PR updates filter_none to recursively remove None values. I added some tests to ensure that values like "", 0 and [] are not removed. This PR should fix the Groq issue and will likely avoid some other errors in the future.


(*) regarding why we don't exactly follow the OpenAI specs in our inference types is that we don't have a good way to generate Union type. If a schema is defined as "oneOf", our script will generate a class that is in fact a superset of all possible values, with all of them being optional. See huggingface/huggingface.js#1537 or glideapps/quicktype#1266 for more details. This issue might be annoying on the long run but there is no easy way to address it. Pydantic defines discriminators unions for that which we could also implement. What's harder is to have a reliable and generic parse_obj method extending a Union type (which doesn't seem possible). Let's see if/when it becomes for urgent to tackle it.

cc @Vaibhavs10 @burtenshaw

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Contributor

@hanouticelina hanouticelina left a comment

Choose a reason for hiding this comment

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

Thank you! I pushed additional commits that should solve the issue you mentioned in the PR description, I added additional test cases as well.
I tested tiny-agents with the PR, it works as expected.

Copy link
Contributor Author

@Wauplin Wauplin left a comment

Choose a reason for hiding this comment

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

Left some minor comments but the logic itself looks good to me. I can't approve the PR since it's originally mine but LGTM once comments are addressed ✔️

),
],
)
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 :)

@hanouticelina hanouticelina merged commit 9b8c73a into main Jun 25, 2025
29 checks passed
@hanouticelina hanouticelina deleted the recursive-filter-none branch June 25, 2025 11:09
hanouticelina added a commit that referenced this pull request Jun 25, 2025
* Recursive filter_none in Inference Providers

* filter none values from messages as well

* update filter_none and add test cases

* don't drop empty dicts in list

* better typing and refactor logic into a list of comrpehension

---------

Co-authored-by: Celina Hanouti <hanouticelina@gmail.com>
mintyleaf pushed a commit to Swarmind/huggingface_hub that referenced this pull request Jul 11, 2025
* Recursive filter_none in Inference Providers

* filter none values from messages as well

* update filter_none and add test cases

* don't drop empty dicts in list

* better typing and refactor logic into a list of comrpehension

---------

Co-authored-by: Celina Hanouti <hanouticelina@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants