Skip to content

Commit 690a57a

Browse files
CopilotOEvortex
andcommitted
Fix tool conversion error handling and improve HTTP 400 guidance
Co-authored-by: OEvortex <158988478+OEvortex@users.noreply.github.com>
1 parent f3cdc64 commit 690a57a

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

HelpingAI/client.py

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,31 @@ def _request(
146146
raise InvalidModelError(model_name, response.status_code, response.headers)
147147
else:
148148
raise InvalidModelError("Unknown model", response.status_code, response.headers)
149-
# Add hint for generic 400 errors when not streaming
150-
if not stream and "Request failed with status code" in error_message:
151-
error_message += ". This model or endpoint might require streaming. Try setting stream=True."
152-
raise InvalidRequestError(error_message, status_code=response.status_code, headers=response.headers)
149+
150+
# Enhanced guidance for 400 errors
151+
enhanced_message = error_message
152+
if not stream:
153+
# Check for various indicators that streaming might be required
154+
streaming_indicators = [
155+
"Request failed with status code",
156+
"streaming",
157+
"stream",
158+
"tool", # Some tool-related requests might require streaming
159+
"function" # Function calling might require streaming
160+
]
161+
162+
if any(indicator in error_message.lower() for indicator in streaming_indicators):
163+
enhanced_message += (
164+
". This model or endpoint might require streaming. "
165+
"Try setting stream=True in your request."
166+
)
167+
else:
168+
enhanced_message += (
169+
". If this error persists, try setting stream=True or "
170+
"check your request parameters."
171+
)
172+
173+
raise InvalidRequestError(enhanced_message, status_code=response.status_code, headers=response.headers)
153174
elif response.status_code == 429:
154175
raise TooManyRequestsError(response.status_code, response.headers)
155176
elif response.status_code == 503:
@@ -519,13 +540,43 @@ def _convert_tools_parameter(
519540
return ensure_openai_format(tools)
520541
except ImportError:
521542
# Fallback if tools module not available - treat as legacy format
543+
import warnings
544+
warnings.warn(
545+
"Tools module not available. Install optional dependencies with: pip install 'HelpingAI[mcp]'. "
546+
"Using legacy tool format."
547+
)
522548
if isinstance(tools, list):
523549
return tools
524550
return None
525551
except Exception as e:
526-
# Log warning but don't break existing functionality
552+
# Enhanced error handling with better guidance
527553
import warnings
528-
warnings.warn(f"Tool conversion failed: {e}. Using legacy behavior.")
554+
error_msg = str(e)
555+
556+
# Provide more helpful error messages based on the error type
557+
if "Unknown built-in tool" in error_msg:
558+
available_tools = "code_interpreter, web_search"
559+
warnings.warn(
560+
f"Tool conversion failed: {e}. "
561+
f"Available built-in tools: {available_tools}. "
562+
f"For custom tools, use OpenAI tool format. Using legacy behavior."
563+
)
564+
elif "Unsupported tool item type" in error_msg:
565+
warnings.warn(
566+
f"Tool conversion failed: {e}. "
567+
f"Tools must be strings (built-in tool names), dicts (OpenAI format), "
568+
f"or MCP server configs. Using legacy behavior."
569+
)
570+
elif "Unsupported tools format" in error_msg:
571+
warnings.warn(
572+
f"Tool conversion failed: {e}. "
573+
f"Supported formats: None, string (category), List[Dict] (OpenAI format), "
574+
f"List[str] (built-in tools), or List[Fn]. Using legacy behavior."
575+
)
576+
else:
577+
warnings.warn(f"Tool conversion failed: {e}. Using legacy behavior.")
578+
579+
# Fallback to legacy behavior - return tools as-is if it's a list
529580
if isinstance(tools, list):
530581
return tools
531582
return None

HelpingAI/tools/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
)
2626
from .compatibility import (
2727
ensure_tool_format,
28+
ensure_openai_format,
2829
convert_legacy_tools,
2930
merge_tool_lists,
3031
create_fn_from_tool_dict,
@@ -58,6 +59,7 @@
5859

5960
# Compatibility utilities
6061
"ensure_tool_format",
62+
"ensure_openai_format",
6163
"convert_legacy_tools",
6264
"merge_tool_lists",
6365
"create_fn_from_tool_dict",

0 commit comments

Comments
 (0)