Skip to content

Replace ChatMessage.from_hf_api with from_dict with asdict #1076

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

Conversation

albertvillanova
Copy link
Member

@albertvillanova albertvillanova commented Mar 25, 2025

Replace ChatMessage.from_hf_api with from_dict with asdict.

TODO:

  • Deprecate ChatMessage.from_hf_api

Follow up to:

@albertvillanova albertvillanova marked this pull request as draft March 25, 2025 17:46
@albertvillanova albertvillanova marked this pull request as ready for review March 28, 2025 17:03
Copy link
Collaborator

@aymeric-roucher aymeric-roucher 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, nice simplifying change!

@classmethod
def from_hf_api(cls, tool_call: "ChatCompletionOutputToolCall") -> "ChatMessageToolCall":
warnings.warn(
"ChatMessageToolCall.from_hf_api is deprecated and will be removed in version 2.0.0. "
Copy link
Collaborator

Choose a reason for hiding this comment

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

Given that we're not sure to do a 2.0.0 soon, isn't it better to indicate a medium version, like 1.16?

Copy link
Member Author

Choose a reason for hiding this comment

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

Breaking changes are typically only allowed in major versions to ensure stability and backward compatibility. If a change introduces breaking modifications, it would generally be reserved for a 2.0.0 release rather than a minor version like 1.16.

Copy link
Collaborator

Choose a reason for hiding this comment

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

In transformers I saw breaking changes being planned for a few minor releases in advance, as we did here: huggingface/transformers#36415 The issue I see with waiting for a major release is that we're not planning to do one soon, while we should probably plan to deprecate from_hf_api, WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

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

If you think it is OK to introduce breaking changes in minor versions...
I just take note to inform users who ask about API stability.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe we could also agree in the deprecation cycle duration: you are proposing next_minor + 3 versions, is it OK?

@albertvillanova
Copy link
Member Author

A problem I did not foresee: after huggingface-hub-0.30.0 (https://pypi.org/project/huggingface-hub/0.30.0/) the dataclass fields are no longer aligned with the corrsponding smolagents dataclass: https://github.com/huggingface/huggingface_hub/pull/2832/files#diff-e3fbb4c028015b8d172390c8acd9b0d4ca985d05c639fdb74c598a506152db6eR208

  • their ChatCompletionOutputMessage now includes the additional field tool_call_id, besides role, content and tool_calls

We need to decide how to handle this discrepancy:

  • Align with huggingface_hub by updating our dataclass to include tool_call_id.
  • Filter out the extra field(s) when interacting with huggingface_hub

@aymeric-roucher, do you have a preference on the best approach here?

@aymeric-roucher
Copy link
Collaborator

@albertvillanova let's drop the tool_call_id: I don't see how it really improves agent performance (a tool call id is useful if you have several tool calls to track which one give which output: here given that it's above the tool_calls level, it's not attached to any particular tool call) + it's not in openai specification.

@albertvillanova
Copy link
Member Author

It is what I did. We should maybe ask to the transformers/huggingface-hub teams why they added that field...

@albertvillanova albertvillanova merged commit 0f4fc59 into huggingface:main Apr 4, 2025
3 checks passed
@Wauplin
Copy link
Contributor

Wauplin commented Apr 8, 2025

More details on why the tool_call_id is needed for some models, and hence added to TGI, and hence added to the huggingface_hub's types: https://huggingface.slack.com/archives/C06JKEMK6BZ/p1739982025532149 (private link)

I don't have much more context to provide as I was not involved in this addition. Better to ask in Slack if there is any limitations in smolagents around that field.

@albertvillanova
Copy link
Member Author

Thanks so much for the context, @Wauplin: we had missed that. Looking into it...

@albertvillanova albertvillanova deleted the use-chat-message-from-dict branch April 8, 2025 09:56
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