{"record":{"id":"0141e0b7e3a3206c","repo":"BerriAI/litellm","slug":"json-dumps-completion-response-0141e0","errorCode":null,"errorMessage":"{json.dumps(completion_response)}","messagePattern":"\\{json\\.dumps\\(completion_response\\)\\}","errorType":"http","errorClass":"NLPCloudError","httpStatus":null,"severity":"error","filePath":"litellm/llms/nlp_cloud/chat/transformation.py","lineNumber":203,"sourceCode":"            additional_args={\"complete_input_dict\": request_data},\n        )\n\n        ## RESPONSE OBJECT\n        try:\n            completion_response: Final = raw_response.json()\n        except Exception:\n            raise NLPCloudError(message=raw_response.text, status_code=raw_response.status_code)\n        if \"error\" in completion_response:\n            raise NLPCloudError(\n                message=completion_response[\"error\"],\n                status_code=raw_response.status_code,\n            )\n        else:\n            try:\n                if len(completion_response[\"generated_text\"]) > 0:\n                    model_response.choices[0].message.content = completion_response[\"generated_text\"]\n            except Exception:\n                raise NLPCloudError(\n                    message=json.dumps(completion_response),\n                    status_code=raw_response.status_code,\n                )\n\n        ## CALCULATING USAGE - baseten charges on time, not tokens - have some mapping of cost here.\n        prompt_tokens: Final = completion_response[\"nb_input_tokens\"]\n        completion_tokens: Final = completion_response[\"nb_generated_tokens\"]\n\n        model_response.created = int(time.time())\n        model_response.model = model\n        usage: Final = Usage(\n            prompt_tokens=prompt_tokens,\n            completion_tokens=completion_tokens,\n            total_tokens=prompt_tokens + completion_tokens,\n        )\n        setattr(model_response, \"usage\", usage)\n        return model_response\n","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/nlp_cloud/chat/transformation.py#L185-L221","documentation":"Raised by litellm's NLPCloud chat transformer when extracting the response fields fails — typically completion_response has no \"generated_text\" key (so the indexing raises), meaning NLP Cloud returned valid JSON that is neither a normal completion nor an error object. The whole JSON body is serialized into the message for debugging.","triggerScenarios":"Calling litellm.completion() with nlp_cloud/* where the JSON body lacks generated_text: an unexpected status shape (e.g. an async job acknowledgement), a changed API contract after an NLP Cloud update, or a gateway JSON error without an \"error\" key.","commonSituations":"NLP Cloud API changes/updates altering response shape, using a completion model with a stream-style response while streaming is off, or account-level notices returned as JSON.","solutions":["Inspect the exception message — it is json.dumps of the full upstream body, showing exactly which shape came back.","Match your call mode to the engine (non-streaming completion vs streaming) as documented for that engine.","Pin/verify the engine name and NLP Cloud API version you coded against.","If the shape changed upstream, update litellm to the latest version which may already handle it."],"exampleFix":"# before\nout = litellm.completion(model=\"nlp_cloud/finetuned-llama-3-70b\", messages=msgs)  # shape drift crashes parse\n\n# after — surface the raw body for diagnosis and degrade gracefully\nfrom litellm.exceptions import APIError\ntry:\n    out = litellm.completion(model=\"nlp_cloud/finetuned-llama-3-70b\", messages=msgs)\nexcept APIError as e:\n    log.error(\"nlp_cloud unexpected body: %s\", e)\n    raise","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"from litellm.exceptions import APIError\ntry:\n    out = litellm.completion(model=\"nlp_cloud/finetuned-llama-3-70b\", messages=msgs)\nexcept APIError as e:\n    # message is json.dumps of the raw body — log it and alert, shape changed upstream\n    log.error(\"nlp_cloud unexpected response shape: %s\", e)\n    raise","preventionTips":["Pin the litellm version you tested against NLP Cloud.","Log the serialized body from this error; it is the fastest way to detect API drift.","Keep a canary call to each provider in monitoring to catch contract changes."],"tags":["nlp-cloud","chat","response-shape","parsing"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}