{"record":{"id":"40326f1a06acc12a","repo":"BerriAI/litellm","slug":"invalid-response-object-traceback-format-exc","errorCode":null,"errorMessage":"Invalid response object {traceback.format_exc()}\n\nreceived_args={received_args}","messagePattern":"Invalid response object (.+?)\n\nreceived_args=(.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py","lineNumber":877,"sourceCode":"                model_response_object.results = response_object[\"results\"]\n\n            return model_response_object\n    except Exception as e:\n        from litellm.exceptions import APIError\n\n        if isinstance(e, APIError):\n            raise\n\n        received_args: Final = dict(\n            response_object=response_object,\n            model_response_object=model_response_object,\n            response_type=response_type,\n            stream=stream,\n            start_time=start_time,\n            end_time=end_time,\n            convert_tool_call_to_json_mode=convert_tool_call_to_json_mode,\n        )\n        raise Exception(f\"Invalid response object {traceback.format_exc()}\\n\\nreceived_args={received_args}\")\n","sourceCodeStart":859,"sourceCodeEnd":878,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py#L859-L878","documentation":"The outer except of convert_dict_to_response re-raises any APIError untouched but wraps every other exception in Exception('Invalid response object <full traceback> ... received_args={...}'). It is a catch-all: the real cause is whatever exception the traceback inside the message shows (KeyError on a missing field, ValidationError from pydantic, TypeError from a bad field type, etc.). The received_args dump includes the full response_object, so inspect it to find the malformed field.","triggerScenarios":"A choice dict missing 'message' or 'delta' (KeyError); provider fields with wrong types that pydantic rejects (e.g. usage tokens as strings); unexpected extra keys or nulls in choices; any exception raised by the per-response-type conversion branches above.","commonSituations":"Custom providers emitting nearly-OpenAI payloads with subtle type mismatches; provider API changes adding/changing field types; cache-replayed responses that lost fields; test fixtures hand-written dicts with typos.","solutions":["Read the embedded traceback in the message string — it names the actual failing field and exception type; fix that field first.","Inspect received_args.response_object in the message for the offending value's shape.","Compare the provider payload field-by-field against OpenAI's response schema (types included) and coerce mismatches in your handler.","If the payload comes from cache, invalidate it and re-fetch."],"exampleFix":"// before (custom handler)\n{'choices':[{'message':{'role':'assistant','content':'ok'},'finish_reason':'stop'}], 'usage':{'prompt_tokens':'5','completion_tokens':'7'}}  # tokens as strings\n\n# after\n{'choices':[{'message':{'role':'assistant','content':'ok'},'finish_reason':'stop'}], 'usage':{'prompt_tokens':5,'completion_tokens':7}}","handlingStrategy":"try-catch","validationCode":"def looks_like_openai_chat(d: dict) -> bool:\n    try:\n        c = d['choices'][0]\n        return isinstance(c.get('message'), dict)\n    except (KeyError, IndexError, TypeError):\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    resp = convert_dict_to_response(response_object=d, response_type='completion')\nexcept Exception as e:\n    if 'Invalid response object' in str(e):\n        # traceback of the real cause is embedded in the message — log it whole\n        logging.exception('conversion failed for payload: %r', d)\n        raise","preventionTips":["The real cause is inside the embedded traceback — always read the full message before guessing.","Coerce numeric usage fields to ints in custom handlers.","Diff provider payloads against OpenAI schemas in CI when integrating new providers."],"tags":["response-conversion","catch-all","schema-mismatch","debugging"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}