{"record":{"id":"89b0dab9e2af61e9","repo":"BerriAI/litellm","slug":"unexpected-response-format-expected-list-or-dict","errorCode":null,"errorMessage":"Unexpected response format. Expected list or dict with 'embedding' key, got: {type(response_data).__name__}","messagePattern":"Unexpected response format\\. Expected list or dict with 'embedding' key, got: (.+?)","errorType":"http","errorClass":"SagemakerError","httpStatus":500,"severity":"error","filePath":"litellm/llms/sagemaker/embedding/transformation.py","lineNumber":112,"sourceCode":"        Transform embedding response for Hugging Face models on SageMaker\n        \"\"\"\n        try:\n            response_data: Final = raw_response.json()\n        except Exception as e:\n            raise SagemakerError(\n                message=f\"Failed to parse response: {e}\",\n                status_code=raw_response.status_code,\n            )\n\n        # Handle both raw array format (TEI) and wrapped format (standard HF)\n        if isinstance(response_data, list):\n            # TEI and some HF models return raw embedding arrays directly\n            embeddings = response_data\n        elif isinstance(response_data, dict) and \"embedding\" in response_data:\n            # Standard HF format with \"embedding\" key\n            embeddings = response_data[\"embedding\"]\n        else:\n            raise SagemakerError(\n                status_code=500,\n                message=f\"Unexpected response format. Expected list or dict with 'embedding' key, got: {type(response_data).__name__}\",\n            )\n\n        if not isinstance(embeddings, list):\n            raise SagemakerError(\n                status_code=422,\n                message=f\"HF response not in expected format - {embeddings}\",\n            )\n\n        output_data: Final = []\n        for idx, embedding in enumerate(embeddings):\n            output_data.append({\"object\": \"embedding\", \"index\": idx, \"embedding\": embedding})\n\n        model_response.object = \"list\"\n        model_response.data = output_data\n        model_response.model = model\n","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/sagemaker/embedding/transformation.py#L94-L130","documentation":"After successfully JSON-parsing a SageMaker embedding response, LiteLLM accepts either a raw list (TEI style) or a dict containing an 'embedding' key (HF inference-dict style). Anything else - a dict without 'embedding', a bare string, a number - raises SagemakerError 500 naming the offending Python type.","triggerScenarios":"The endpoint returns JSON like {'embeddings': [...]}, {'error': {...}} with HTTP 200, or a scalar/string body; i.e. a schema the transformer never anticipated.","commonSituations":"Custom HF inference containers returning their own key name; newer TEI versions changing the response envelope; models that wrap results in extra metadata objects.","solutions":["Inspect the type named in the message, then log the raw body to see the actual key structure.","If the key differs (e.g. 'embeddings'), change the container to return either a bare list or {'embedding': [[...], ...]}.","For TEI deployments, confirm you are hitting the /embed route and not another route with a different response shape."],"exampleFix":"# custom container - before\nreturn {'embeddings': vectors}\n# after (schema LiteLLM understands)\nreturn {'embedding': vectors}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"from litellm import SagemakerError\n\ntry:\n    resp = litellm.embedding(model='sagemaker/hf-emb', input=texts)\nexcept SagemakerError as e:\n    if 'Unexpected response format' in str(e):\n        log.error('embedding schema changed: %s', e.message)\n    raise","preventionTips":["Freeze the serving container version so the response envelope is stable.","Contract-test the container response shape on every redeploy."],"tags":["sagemaker","embedding","response-parsing","tei"],"backgroundTag":"unexpected-response-schema","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}