fix: prevent FinalAnswerException from being caught by except Exception by majiayu000 · Pull Request #1914 · huggingface/smolagents (original) (raw)
Navigation Menu
Appearance settings
- AI CODE CREATION
* GitHub CopilotWrite better code with AI
* GitHub SparkBuild and deploy intelligent apps
* GitHub ModelsManage and compare prompts
* MCP RegistryNewIntegrate external tools - DEVELOPER WORKFLOWS
* ActionsAutomate any workflow
* CodespacesInstant dev environments
* IssuesPlan and track work
* Code ReviewManage code changes - APPLICATION SECURITY
* GitHub Advanced SecurityFind and fix vulnerabilities
* Code securitySecure your code as you build
* Secret protectionStop leaks before they start - EXPLORE
* Why GitHub
* Documentation
* Blog
* Changelog
* Marketplace
- AI CODE CREATION
- BY COMPANY SIZE
* Enterprises
* Small and medium teams
* Startups
* Nonprofits - BY USE CASE
* App Modernization
* DevSecOps
* DevOps
* CI/CD
* View all use cases - BY INDUSTRY
* Healthcare
* Financial services
* Manufacturing
* Government
* View all industries
- BY COMPANY SIZE
- EXPLORE BY TOPIC
* AI
* Software Development
* DevOps
* Security
* View all topics - EXPLORE BY TYPE
* Customer stories
* Events & webinars
* Ebooks & reports
* Business insights
* GitHub Skills - SUPPORT & SERVICES
* Documentation
* Customer support
* Community forum
* Trust center
* Partners
- EXPLORE BY TOPIC
- COMMUNITY
* GitHub SponsorsFund open source developers - PROGRAMS
* Security Lab
* Maintainer Community
* Accelerator
* GitHub Stars
* Archive Program - REPOSITORIES
* Topics
* Trending
* Collections
- COMMUNITY
- Pricing
Provide feedback
We read every piece of feedback, and take your input very seriously.
Include my email address so I can be contacted
Saved searches
Use saved searches to filter your results more quickly
Appearance settings
huggingface / smolagents Public
Notifications You must be signed in to change notification settings
Additional navigation options
Merged
aymeric-roucher merged 1 commit intohuggingface:mainfrom
majiayu000:fix/final-answer-exception-inheritance
Dec 23, 2025
ConversationCommits (1)ChecksFiles changed
Merged
fix: prevent FinalAnswerException from being caught by except Exception#1914
aymeric-roucher merged 1 commit intohuggingface:mainfrom
majiayu000:fix/final-answer-exception-inheritance
Conversation
Copy link Copy Markdown
Contributor
majiayu000 commented
Summary
- Changed
FinalAnswerExceptionto inherit fromBaseExceptioninstead ofException - This prevents agent-generated code with
try/except Exceptionblocks from incorrectly catching the exception - Added test case to verify the fix
Test plan
- Added test
test_final_answer_not_caught_by_except_exception - All existing tests pass (
make test) - Code quality checks pass (
make quality)
Fixes #1905
aymeric-roucher reacted with heart emoji
`` [fix: prevent FinalAnswerException from being caught by except Exception](/huggingface/smolagents/pull/1914/commits/fac55deb022edbfca75ccb2e261cc2b41224ce8d "fix: prevent FinalAnswerException from being caught by except Exception
FinalAnswerException now inherits from BaseException instead of Exception
to prevent agent-generated code with try/except Exception blocks from
incorrectly catching the exception when final_answer() is called.
Fixes #1905") ``
[fac55de](/huggingface/smolagents/pull/1914/commits/fac55deb022edbfca75ccb2e261cc2b41224ce8d)
FinalAnswerException now inherits from BaseException instead of Exception
to prevent agent-generated code with try/except Exception blocks from
incorrectly catching the exception when final_answer() is called.
Fixes huggingface#1905
aymeric-roucher approved these changes Dec 23, 2025
Copy link Copy Markdown
Collaborator
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!
aymeric-roucher merged commit 1af0799 into huggingface:main
3 checks passed
Copy link Copy Markdown
Contributor Author
majiayu000 commented
Happy to help!
aymeric-roucher reacted with hooray emoji
majiayu000 deleted the fix/final-answer-exception-inheritance branch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
[ Show hidden characters]({{ revealButtonHref }})
Sign up for free to join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
aymeric-roucher aymeric-roucher approved these changes
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
BUG: Agent code should not be able to catch FinalAnswerException