MCPcopy Index your code
hub / github.com/abetlen/llama-cpp-python / _coerce_tool_arguments

Method _coerce_tool_arguments

examples/server/server.py:7076–7133  ·  view source on GitHub ↗
(
        self,
        tool_name: str,
        arguments: Dict[str, str],
        *,
        partial: bool,
    )

Source from the content-addressed store, hash-verified

7074 return decoded
7075
7076 def _coerce_tool_arguments(
7077 self,
7078 tool_name: str,
7079 arguments: Dict[str, str],
7080 *,
7081 partial: bool,
7082 ) -> Dict[str, Any]:
7083 if self._tool_content_type(tool_name) == "text":
7084 raw_input = arguments.get("input", "")
7085 if not isinstance(raw_input, str):
7086 raw_input = str(raw_input)
7087 return {"input": raw_input}
7088 if self._tools is None:
7089 raise CompletionResponseParsingError(
7090 f"response included tool call '{tool_name}' but the request declared no tools"
7091 )
7092 tool = next(
7093 (
7094 candidate
7095 for candidate in self._tools
7096 if candidate.get("type") == "function"
7097 and candidate.get("function", {}).get("name") == tool_name
7098 ),
7099 None,
7100 )
7101 if tool is None:
7102 raise CompletionResponseParsingError(
7103 f"response included undeclared tool call '{tool_name}'"
7104 )
7105 parameters = tool.get("function", {}).get("parameters") or {
7106 "type": "object",
7107 "properties": {},
7108 "required": [],
7109 }
7110 properties = parameters.get("properties", {})
7111 coerced: Dict[str, Any] = {}
7112 for argument_name, raw_value in arguments.items():
7113 if argument_name not in properties:
7114 raise CompletionResponseParsingError(
7115 f"tool '{tool_name}' returned unexpected argument '{argument_name}'"
7116 )
7117 coerced[argument_name] = self._coerce_tool_argument(
7118 raw_value,
7119 properties[argument_name],
7120 tool_name=tool_name,
7121 argument_name=argument_name,
7122 )
7123 if not partial:
7124 missing = [
7125 argument_name
7126 for argument_name in parameters.get("required", [])
7127 if argument_name not in coerced
7128 ]
7129 if missing:
7130 raise CompletionResponseParsingError(
7131 f"tool '{tool_name}' response is missing required arguments: {', '.join(missing)}"
7132 )
7133 return coerced

Callers 1

Calls 3

_tool_content_typeMethod · 0.95
_coerce_tool_argumentMethod · 0.95

Tested by

no test coverage detected