Skip to content

[MCP] reinject JSON parse & runtime tool errors back into the chat history #3137

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
Jun 4, 2025

Conversation

hanouticelina
Copy link
Contributor

Copy-paste from the JS PR: huggingface/huggingface.js#1511 which I find useful.

this should prevents crashes when the model emits malformed JSON and gives the LLM immediate feedback it can reason over, matching the js implem.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enhances process_single_turn_with_tools to catch and re-inject JSON parsing and tool execution errors back into the chat history, preventing crashes when malformed JSON is produced or tool calls fail.

  • Add JSONDecodeError handler to wrap malformed JSON from the model and yield an error message
  • Wrap tool execution in a try/except to catch runtime errors and report them as tool messages
Comments suppressed due to low confidence (2)

src/huggingface_hub/inference/_mcp/mcp_client.py:315

  • Introduce unit tests covering the JSONDecodeError branch to verify that malformed JSON from the model is caught and properly yielded as a tool message.
except json.JSONDecodeError as err:

src/huggingface_hub/inference/_mcp/mcp_client.py:343

  • [nitpick] Consider standardizing the error message format and avoid leaking raw exception text; for example, use a concise message and log the full exception separately.
tool_message["content"] = f"Error: MCP tool call failed with error message: {err}"

Comment on lines +342 to +343
except Exception as err:
tool_message["content"] = f"Error: MCP tool call failed with error message: {err}"
Copy link
Preview

Copilot AI Jun 3, 2025

Choose a reason for hiding this comment

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

Avoid catching the broad Exception; consider handling specific exceptions thrown by session.call_tool or re-raising unexpected errors to avoid masking serious issues.

Suggested change
except Exception as err:
tool_message["content"] = f"Error: MCP tool call failed with error message: {err}"
except (ValueError, TypeError, json.JSONDecodeError) as err:
tool_message["content"] = f"Error: MCP tool call failed with error message: {err}"
except Exception as err:
logger.error(f"Unexpected error during MCP tool call: {err}")
raise

Copilot uses AI. Check for mistakes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree in general but disagree on this specific use case. Let's keep it like this

@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

@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.

Thanks!

Comment on lines +342 to +343
except Exception as err:
tool_message["content"] = f"Error: MCP tool call failed with error message: {err}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Agree in general but disagree on this specific use case. Let's keep it like this

@hanouticelina hanouticelina merged commit 987dadc into main Jun 4, 2025
28 checks passed
@hanouticelina hanouticelina deleted the reinject-LLMs-errors branch June 4, 2025 14:28
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