renamed clean schema to alternate schema
This commit is contained in:
@@ -524,8 +524,8 @@ class GeminiMultimodalLiveLLMService(LLMService):
|
|||||||
"""
|
"""
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def needs_mcp_clean_schema(self) -> bool:
|
def needs_mcp_alternate_schema(self) -> bool:
|
||||||
"""Check if this LLM service requires MCP schema cleaning.
|
"""Check if this LLM service requires alternate MCP schema.
|
||||||
|
|
||||||
Google/Gemini has stricter JSON schema validation and requires
|
Google/Gemini has stricter JSON schema validation and requires
|
||||||
certain properties to be removed or modified for compatibility.
|
certain properties to be removed or modified for compatibility.
|
||||||
|
|||||||
@@ -631,8 +631,8 @@ class GoogleLLMService(LLMService):
|
|||||||
"""
|
"""
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def needs_mcp_clean_schema(self) -> bool:
|
def needs_mcp_alternate_schema(self) -> bool:
|
||||||
"""Check if this LLM service requires MCP schema cleaning.
|
"""Check if this LLM service requires alternate MCP schema.
|
||||||
|
|
||||||
Google/Gemini has stricter JSON schema validation and requires
|
Google/Gemini has stricter JSON schema validation and requires
|
||||||
certain properties to be removed or modified for compatibility.
|
certain properties to be removed or modified for compatibility.
|
||||||
|
|||||||
@@ -307,8 +307,8 @@ class LLMService(AIService):
|
|||||||
return True
|
return True
|
||||||
return function_name in self._functions.keys()
|
return function_name in self._functions.keys()
|
||||||
|
|
||||||
def needs_mcp_clean_schema(self) -> bool:
|
def needs_mcp_alternate_schema(self) -> bool:
|
||||||
"""Check if this LLM service requires MCP schema cleaning.
|
"""Check if this LLM service requires alternate MCP schema.
|
||||||
|
|
||||||
Some LLM services have stricter JSON schema validation and require
|
Some LLM services have stricter JSON schema validation and require
|
||||||
certain properties to be removed or modified for compatibility.
|
certain properties to be removed or modified for compatibility.
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ class MCPClient(BaseObject):
|
|||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
self._server_params = server_params
|
self._server_params = server_params
|
||||||
self._session = ClientSession
|
self._session = ClientSession
|
||||||
self._needs_schema_cleaning = False
|
self._needs_alternate_schema = False
|
||||||
|
|
||||||
if isinstance(server_params, StdioServerParameters):
|
if isinstance(server_params, StdioServerParameters):
|
||||||
self._client = stdio_client
|
self._client = stdio_client
|
||||||
@@ -79,47 +79,47 @@ class MCPClient(BaseObject):
|
|||||||
Returns:
|
Returns:
|
||||||
A ToolsSchema containing all successfully registered tools.
|
A ToolsSchema containing all successfully registered tools.
|
||||||
"""
|
"""
|
||||||
# Check once if the LLM needs schema cleaning
|
# Check once if the LLM needs alternate strict schema
|
||||||
self._needs_schema_cleaning = llm and llm.needs_mcp_clean_schema()
|
self._needs_alternate_schema = llm and llm.needs_mcp_alternate_schema()
|
||||||
tools_schema = await self._register_tools(llm)
|
tools_schema = await self._register_tools(llm)
|
||||||
return tools_schema
|
return tools_schema
|
||||||
|
|
||||||
def _clean_schema_for_strict_validation(self, schema: Dict[str, Any]) -> Dict[str, Any]:
|
def _get_alternate_schema_for_strict_validation(self, schema: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
"""Clean a JSON schema to be compatible with LLMs that have strict validation.
|
"""Get an alternate JSON schema to be compatible with LLMs that have strict validation.
|
||||||
|
|
||||||
Some LLMs have stricter validation and don't allow certain schema properties
|
Some LLMs have stricter validation and don't allow certain schema properties
|
||||||
that are valid in standard JSON Schema.
|
that are valid in standard JSON Schema.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
schema: The JSON schema to clean
|
schema: The JSON schema to get an alternate schema for
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
A cleaned schema compatible with strict validation
|
An alternate schema compatible with strict validation
|
||||||
"""
|
"""
|
||||||
if not isinstance(schema, dict):
|
if not isinstance(schema, dict):
|
||||||
return schema
|
return schema
|
||||||
|
|
||||||
cleaned = {}
|
alternate_schema = {}
|
||||||
|
|
||||||
for key, value in schema.items():
|
for key, value in schema.items():
|
||||||
# Skip additionalProperties as some LLMs don't like additionalProperties: false
|
# Skip additionalProperties as some LLMs don't like additionalProperties: false
|
||||||
if key == "additionalProperties":
|
if key == "additionalProperties":
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Recursively clean nested objects
|
# Recursively get alternate schema for nested objects
|
||||||
if isinstance(value, dict):
|
if isinstance(value, dict):
|
||||||
cleaned[key] = self._clean_schema_for_strict_validation(value)
|
alternate_schema[key] = self._get_alternate_schema_for_strict_validation(value)
|
||||||
elif isinstance(value, list):
|
elif isinstance(value, list):
|
||||||
cleaned[key] = [
|
alternate_schema[key] = [
|
||||||
self._clean_schema_for_strict_validation(item)
|
self._get_alternate_schema_for_strict_validation(item)
|
||||||
if isinstance(item, dict)
|
if isinstance(item, dict)
|
||||||
else item
|
else item
|
||||||
for item in value
|
for item in value
|
||||||
]
|
]
|
||||||
else:
|
else:
|
||||||
cleaned[key] = value
|
alternate_schema[key] = value
|
||||||
|
|
||||||
return cleaned
|
return alternate_schema
|
||||||
|
|
||||||
def _convert_mcp_schema_to_pipecat(
|
def _convert_mcp_schema_to_pipecat(
|
||||||
self, tool_name: str, tool_schema: Dict[str, Any]
|
self, tool_name: str, tool_schema: Dict[str, Any]
|
||||||
@@ -138,10 +138,10 @@ class MCPClient(BaseObject):
|
|||||||
properties = tool_schema["input_schema"].get("properties", {})
|
properties = tool_schema["input_schema"].get("properties", {})
|
||||||
required = tool_schema["input_schema"].get("required", [])
|
required = tool_schema["input_schema"].get("required", [])
|
||||||
|
|
||||||
# Only clean properties for LLMs that need strict schema validation
|
# Only get alternate schema for LLMs that need strict schema validation
|
||||||
if self._needs_schema_cleaning:
|
if self._needs_alternate_schema:
|
||||||
logger.debug("Cleaning schema for strict validation")
|
logger.debug("Getting alternate schema for strict validation")
|
||||||
properties = self._clean_schema_for_strict_validation(properties)
|
properties = self._get_alternate_schema_for_strict_validation(properties)
|
||||||
|
|
||||||
schema = FunctionSchema(
|
schema = FunctionSchema(
|
||||||
name=tool_name,
|
name=tool_name,
|
||||||
|
|||||||
Reference in New Issue
Block a user