FIX tavily search results return type#1123
Conversation
Signed-off-by: George Armstrong <georgea@nvidia.com>
Signed-off-by: George Armstrong <georgea@nvidia.com>
Signed-off-by: George Armstrong <georgea@nvidia.com>
📝 WalkthroughWalkthroughThese changes enhance tool result extraction in the MCP client by introducing a helper function to handle multiple content items from tool calls. The Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
nemo_skills/mcp/clients.py (2)
109-121: Consider usingTypeErrorfor invalid content item structure.The static analysis hints suggest using
TypeErrorwhen the item doesn't have the expectedtextattribute, as this is fundamentally a type/structure mismatch rather than an invalid value. This is a minor style consideration.def _extract_item(item) -> Any: """Extract a JSON-serializable value from a single content item. Returns the parsed JSON if text is valid JSON, otherwise the raw text. - Raises ValueError if the item doesn't have a text attribute. + Raises TypeError if the item doesn't have a text attribute. """ text = getattr(item, "text", None) if not isinstance(text, str): - raise ValueError(f"Content item has no text attribute: {item}") + raise TypeError(f"Content item has no text attribute: {item}") try: return json.loads(text) except json.JSONDecodeError: return textNote: If you change to
TypeError, you'd also need to update theexcept ValueErrorin_extract_tool_resultto catchTypeErroras well.
147-153: UseLOG.exceptionto include traceback in error logs.When logging inside an
exceptblock,LOG.exceptionautomatically includes the exception traceback, which aids debugging. As flagged by static analysis.try: if len(content) == 1: return _extract_item(content[0]) return [_extract_item(item) for item in content] except ValueError as e: - LOG.error("Unsupported content type in tool result: %s", e) + LOG.exception("Unsupported content type in tool result: %s", e) return {"error": "Unsupported content type returned from tool"}
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
nemo_skills/mcp/clients.py(1 hunks)tests/test_mcp_clients.py(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
tests/test_mcp_clients.py (1)
nemo_skills/mcp/clients.py (4)
MCPStdioClient(413-470)call_tool(342-343)call_tool(404-410)call_tool(464-470)
🪛 Ruff (0.14.8)
nemo_skills/mcp/clients.py
117-117: Prefer TypeError exception for invalid type
(TRY004)
117-117: Avoid specifying long messages outside the exception class
(TRY003)
152-152: Use logging.exception instead of logging.error
Replace with exception
(TRY400)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: unit-tests
- GitHub Check: pre-commit
🔇 Additional comments (2)
tests/test_mcp_clients.py (1)
610-636: LGTM! Good test coverage for multi-item content extraction.This test effectively validates the new behavior where tools returning lists without explicit return type hints produce multiple content items. The test structure is clean:
- Creates a minimal reproducible server script
- Validates both the return type and content
- Assertion message helps debugging if it fails
One minor suggestion: consider adding an edge case test for a tool returning an empty list to ensure consistent behavior.
nemo_skills/mcp/clients.py (1)
124-153: LGTM! Robust multi-item content extraction.The refactored
_extract_tool_resultfunction properly handles:
- Error results via
isErrorflag- Structured content passthrough
- Single vs multiple content items (key fix for tavily search results)
- Graceful fallback with consistent error dict format
The distinction between returning a single item vs a list based on
len(content)is the correct approach for preserving backward compatibility while supporting multi-item responses.
Signed-off-by: George Armstrong <georgea@nvidia.com>
Signed-off-by: George Armstrong <georgea@nvidia.com> Signed-off-by: wasiahmad <wasiahmad@ucla.edu>
Signed-off-by: George Armstrong <georgea@nvidia.com> Signed-off-by: dlord <dlord@nvidia.com>
Signed-off-by: George Armstrong <georgea@nvidia.com> Signed-off-by: Cheng-Ping Hsieh <chsieh@nvidia.com>
Signed-off-by: George Armstrong <georgea@nvidia.com>
Signed-off-by: George Armstrong <georgea@nvidia.com>
Signed-off-by: George Armstrong <georgea@nvidia.com> Signed-off-by: dgitman <dgitman@nvidia.com>
Summary by CodeRabbit
Bug Fixes
Tests
✏️ Tip: You can customize this high-level summary in your review settings.