(
self,
raw_value: str,
schema: Dict[str, Any],
*,
tool_name: str,
argument_name: str,
)
| 6995 | return None |
| 6996 | |
| 6997 | def _coerce_tool_argument( |
| 6998 | self, |
| 6999 | raw_value: str, |
| 7000 | schema: Dict[str, Any], |
| 7001 | *, |
| 7002 | tool_name: str, |
| 7003 | argument_name: str, |
| 7004 | ) -> Any: |
| 7005 | if "oneOf" in schema: |
| 7006 | last_error: Optional[BaseException] = None |
| 7007 | for variant in schema["oneOf"]: |
| 7008 | try: |
| 7009 | return self._coerce_tool_argument( |
| 7010 | raw_value, |
| 7011 | variant, |
| 7012 | tool_name=tool_name, |
| 7013 | argument_name=argument_name, |
| 7014 | ) |
| 7015 | except BaseException as exc: |
| 7016 | last_error = exc |
| 7017 | raise CompletionResponseParsingError( |
| 7018 | f"tool '{tool_name}' argument '{argument_name}' did not match any allowed schema" |
| 7019 | ) from last_error |
| 7020 | schema_type = schema.get("type") |
| 7021 | if isinstance(schema_type, list): |
| 7022 | last_type_error: Optional[BaseException] = None |
| 7023 | for variant_type in schema_type: |
| 7024 | try: |
| 7025 | return self._coerce_tool_argument( |
| 7026 | raw_value, |
| 7027 | {**schema, "type": variant_type}, |
| 7028 | tool_name=tool_name, |
| 7029 | argument_name=argument_name, |
| 7030 | ) |
| 7031 | except BaseException as exc: |
| 7032 | last_type_error = exc |
| 7033 | raise CompletionResponseParsingError( |
| 7034 | f"tool '{tool_name}' argument '{argument_name}' did not match any allowed type" |
| 7035 | ) from last_type_error |
| 7036 | if schema_type in {None, "string"}: |
| 7037 | return raw_value |
| 7038 | try: |
| 7039 | decoded = json.loads(raw_value) |
| 7040 | except json.JSONDecodeError as exc: |
| 7041 | raise CompletionResponseParsingError( |
| 7042 | f"tool '{tool_name}' argument '{argument_name}' is not valid JSON for type '{schema_type}'" |
| 7043 | ) from exc |
| 7044 | if schema_type == "integer": |
| 7045 | if isinstance(decoded, bool) or not isinstance(decoded, int): |
| 7046 | raise CompletionResponseParsingError( |
| 7047 | f"tool '{tool_name}' argument '{argument_name}' must be an integer" |
| 7048 | ) |
| 7049 | return decoded |
| 7050 | if schema_type == "number": |
| 7051 | if isinstance(decoded, bool) or not isinstance(decoded, (int, float)): |
| 7052 | raise CompletionResponseParsingError( |
| 7053 | f"tool '{tool_name}' argument '{argument_name}' must be a number" |
| 7054 | ) |
no test coverage detected