# openai.BadRequestError: 参数无效：参数'temperature'的值不是有效数字：'hot'

- **ID:** `llm/function-call-argument-type-mismatch`
- **领域:** llm
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

由于模式定义不明确或对参数类型的提示不足，LLM生成了错误数据类型的函数调用参数（例如字符串而非数字）。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| openai==1.30.0 | active | — | — |
| python==3.11 | active | — | — |

## 解决方案

1. ```
   Add explicit type descriptions in the function schema's parameter descriptions: `'temperature': {'type': 'number', 'description': 'A float between 0 and 2, e.g., 0.7. Do NOT use words like hot or cold.'}` and use `enum` constraints if applicable: `'temperature': {'type': 'number', 'enum': [0.0, 0.5, 1.0, 1.5, 2.0]}`
   ```
2. ```
   Implement a validation layer that catches type errors and retries the function call with a corrected prompt: `try: response = client.chat.completions.create(...); except BadRequestError as e: if 'not a valid number' in str(e): corrected_messages = messages + [{'role': 'assistant', 'content': None, 'function_call': ...}, {'role': 'function', 'name': 'validation_error', 'content': f'Type error: {e}'}]; response = retry(corrected_messages)`
   ```

## 无效尝试

- **** — LLMs are not reliably type-safe; they may ignore instructions if the schema is ambiguous or if the model's token distribution favors string outputs. (65% 失败率)
- **** — This is brittle and doesn't scale; the LLM may generate other invalid types (e.g., arrays instead of objects) that the regex cannot handle. (85% 失败率)
- **** — This defeats the purpose of structured tool use and introduces more parsing errors from free-form text. (90% 失败率)
