{"record":{"id":"2342f30fffd9f4ec","repo":"BerriAI/litellm","slug":"json-dumps-event-obj","errorCode":null,"errorMessage":"{json.dumps(event_obj)}","messagePattern":"\\{json\\.dumps\\(event_obj\\)\\}","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/sap/chat/handler.py","lineNumber":88,"sourceCode":"                    for c in (orc.get(\"choices\") or [])\n                ],\n            }\n        )\n\n    @staticmethod\n    def to_openai_chunk(event_obj: dict) -> OpenAIChatCompletionChunk | None:\n        \"\"\"\n        Accepts:\n          - {\"final_result\": <openai-style CHUNK>}   (IMPORTANT: this is just another chunk, NOT terminal)\n          - {\"orchestration_result\": {...}}          (map to chunk)\n          - already-openai-shaped chunks\n          - other events (ignored)\n        Raises:\n          - ValueError for in-stream error objects\n        \"\"\"\n        # In-stream error per spec (surface as exception)\n        if \"code\" in event_obj or \"error\" in event_obj:\n            raise ValueError(json.dumps(event_obj))\n\n        # FINAL RESULT IS *NOT* TERMINAL: treat it as the next chunk\n        if \"final_result\" in event_obj:\n            fr: Final = event_obj[\"final_result\"] or {}\n            # ensure it looks like an OpenAI chunk\n            if \"object\" not in fr:\n                fr[\"object\"] = \"chat.completion.chunk\"\n            return OpenAIChatCompletionChunk.model_validate(fr)\n\n        # Orchestration incremental delta\n        if \"orchestration_result\" in event_obj:\n            return _StreamParser._from_orchestration_result(event_obj)\n\n        # Already an OpenAI-like chunk\n        if \"choices\" in event_obj and \"object\" in event_obj:\n            return OpenAIChatCompletionChunk.model_validate(event_obj)\n\n        # Unknown / heartbeat / metrics","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/sap/chat/handler.py#L70-L106","documentation":"While normalizing SAP Generative AI Hub orchestration stream events into OpenAI-style chunks, LiteLLM treats any event object carrying a 'code' or 'error' key as an in-stream error per the orchestration spec, and raises ValueError containing the full event as JSON. This is how mid-stream failures (moderation blocks, downstream model errors) are surfaced instead of being silently skipped.","triggerScenarios":"Streaming a sap/ chat completion where the orchestration pipeline emits an error event after the stream opened - e.g. the filtering/masking module rejected content, a grounding datasource failed, or the underlying deployment returned an error mid-generation. Also triggered by any non-chunk event that happens to contain 'code' (e.g. metrics events with a code field).","commonSituations":"Azure OpenAI grounding or templating module misconfigured so the orchestration fails after the stream starts; content filters tripping on user input; deployments where the model id in the module config is wrong and the error only surfaces once generation begins.","solutions":["Parse the JSON in the exception message: it contains the SAP error code and details that identify the failing module.","If the code indicates a filtering/masking rejection, adjust the input or the module configuration in the request.","If it names a deployment/model issue, verify model names and that the deployment exists in your AI Core resource group.","Reproduce with stream=False to see whether the same orchestration error appears as a normal HTTP error response."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"import json\n\ntry:\n    for chunk in stream:\n        handle(chunk)\nexcept ValueError as e:\n    try:\n        sap_err = json.loads(str(e))\n    except json.JSONDecodeError:\n        raise\n    log.error('SAP in-stream error code=%s', sap_err.get('code'))","preventionTips":["Validate module configs (filtering, grounding, masking) before opening a stream.","Keep a non-streaming debug mode to reproduce orchestration errors as plain HTTP errors."],"tags":["sap","generative-ai-hub","streaming","error-event"],"backgroundTag":"in-stream-error-event","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}