llm type_error ai_generated true

openai.BadRequestError: Invalid parameter: value for parameter 'temperature' is not a valid number: 'hot'

ID: llm/function-call-argument-type-mismatch

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
1Evidence
2024-04-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
openai==1.30.0 active
python==3.11 active

Root Cause

LLM generated a function call argument with the wrong data type (e.g., string instead of number) due to ambiguous schema definitions or insufficient prompting for parameter types.

generic

中文

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

Official Documentation

https://platform.openai.com/docs/guides/function-calling

Workarounds

  1. 85% success 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]}`
    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. 90% success 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)`
    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)`

中文步骤

  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)`

Dead Ends

Common approaches that don't work:

  1. 65% fail

    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.

  2. 85% fail

    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.

  3. 90% fail

    This defeats the purpose of structured tool use and introduces more parsing errors from free-form text.