Skip to content

Bug Fix: KeyError when agent reaches max steps with image input #1525

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 2 commits into from
Jul 7, 2025

Conversation

robin-ede
Copy link
Contributor

Problem

When an agent reaches max steps with image input, a KeyError occurs because the error handling in provide_final_answer returns a plain string instead of the expected list
format. This causes models.py to fail when trying to pop("image") from the message content.

Solution

Changed the error message format in agents.py:826 from a plain string to a properly structured list with type annotation, matching the expected ChatMessage content format.

Testing

  • All existing tests pass
  • Code quality checks pass (make quality)
  • Code formatting applied (make style)
  • Fix verified with custom test case

Related Issue

Fixes #1524

robin-ede added 2 commits July 5, 2025 23:52
When an agent reaches max steps with image input, the error handling in provide_final_answer was returning a plain string as content instead of the expected list format. This caused a KeyError when models.py tried to pop('image') from the message content during processing.

The fix changes the error message format from a plain string to a properly structured list with type annotation, matching the expected ChatMessage content format.

Fixes huggingface#1524
Copy link
Member

@albertvillanova albertvillanova left a comment

Choose a reason for hiding this comment

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

Thanks, good catch!

I think, the CI test error is unrelated. See:

@albertvillanova albertvillanova merged commit 8682e76 into huggingface:main Jul 7, 2025
1 of 5 checks passed
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.

Error "Error in generating final LLM output: 'image'" when agent reaches max steps with image input
2 participants