{"record":{"id":"c47d2bb98a73df5d","repo":"microsoft/semantic-kernel","slug":"if-response-format-has-type-json-schema-json-s-c47d2b","errorCode":null,"errorMessage":"If response_format has type 'json_schema', 'json_schema' must be a valid dictionary.","messagePattern":"If response_format has type 'json_schema', 'json_schema' must be a valid dictionary\\.","errorType":"exception","errorClass":"ServiceInvalidExecutionSettingsError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/ai/azure_ai_inference/azure_ai_inference_prompt_execution_settings.py","lineNumber":73,"sourceCode":"    @model_validator(mode=\"before\")\n    def validate_response_format_and_set_flag(cls, values: Any) -> Any:\n        \"\"\"Validate the response_format and set structured_json_response accordingly.\"\"\"\n        if not isinstance(values, dict):\n            return values\n        response_format = values.get(\"response_format\", None)\n\n        if response_format is None:\n            return values\n\n        if isinstance(response_format, dict):\n            if response_format.get(\"type\") == \"json_object\":\n                return values\n            if response_format.get(\"type\") == \"json_schema\":\n                json_schema = response_format.get(\"json_schema\")\n                if isinstance(json_schema, dict):\n                    values[\"structured_json_response\"] = True\n                    return values\n                raise ServiceInvalidExecutionSettingsError(\n                    \"If response_format has type 'json_schema', 'json_schema' must be a valid dictionary.\"\n                )\n        if isinstance(response_format, type):\n            if issubclass(response_format, BaseModel):\n                values[\"structured_json_response\"] = True\n            else:\n                values[\"structured_json_response\"] = True\n        else:\n            raise ServiceInvalidExecutionSettingsError(\n                \"response_format must be a dictionary, a subclass of BaseModel, a Python class/type, or None\"\n            )\n\n        return values\n\n\n@experimental\nclass AzureAIInferenceEmbeddingPromptExecutionSettings(PromptExecutionSettings):\n    \"\"\"Azure AI Inference Embedding Prompt Execution Settings.","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/azure_ai_inference/azure_ai_inference_prompt_execution_settings.py#L55-L91","documentation":"Raised by the Azure AI Inference prompt-execution settings validator when response_format is a dict whose 'type' is 'json_schema' but the nested 'json_schema' value is not a dict. The connector needs the schema as a proper JSON-schema dictionary to enable structured output; a string, None, list, or other type cannot be sent as a valid schema definition.","triggerScenarios":"Passing response_format={'type':'json_schema','json_schema': '<json string>'} or omitting/mis-typing the inner 'json_schema' key (e.g. using 'schema' instead of 'json_schema', or passing a Pydantic class instead of its .model_json_schema() dict).","commonSituations":"Confusing OpenAI's response_format shape with a custom one; passing a serialized JSON string instead of a parsed dict; using the wrong key name ('schema' vs 'json_schema'); pasting an example that omits the inner dict.","solutions":["Ensure response_format['json_schema'] is a dict — pass a parsed JSON-schema object, e.g. {'type':'json_schema','json_schema': {'name':'X','schema': {...}}} per the Azure AI Inference shape.","If you have a Pydantic model, either pass the class/type directly (the validator accepts BaseModel subclasses) or call MyModel.model_json_schema() to get the dict.","Double-check the key name is 'json_schema' (not 'schema') and the value is not a string."],"exampleFix":"# before\nsettings.response_format = {\"type\": \"json_schema\", \"json_schema\": '{\"type\":\"object\"}'}\n\n# after\nsettings.response_format = {\n    \"type\": \"json_schema\",\n    \"json_schema\": {\"name\": \"MyObj\", \"schema\": {\"type\": \"object\", \"properties\": {}}},\n}","handlingStrategy":"validation","validationCode":"def validate_json_schema_response_format(rf):\n    if isinstance(rf, dict) and rf.get(\"type\") == \"json_schema\":\n        assert isinstance(rf.get(\"json_schema\"), dict), \\\n            \"json_schema must be a dict when type is 'json_schema'\"\n    return rf\n\nsettings.response_format = validate_json_schema_response_format(settings.response_format)","typeGuard":"def is_valid_json_schema_dict(rf) -> bool:\n    return (\n        isinstance(rf, dict)\n        and rf.get(\"type\") == \"json_schema\"\n        and isinstance(rf.get(\"json_schema\"), dict)\n    )","tryCatchPattern":"from semantic_kernel.exceptions import ServiceInvalidExecutionSettingsError\ntry:\n    settings.response_format = rf\nexcept ServiceInvalidExecutionSettingsError as e:\n    if \"json_schema\" in str(e):\n        settings.response_format = {\"type\": \"json_schema\", \"json_schema\": parsed_schema_dict}","preventionTips":["Always pass the inner schema as a dict, never a JSON string.","Use the key name 'json_schema' (not 'schema').","For Pydantic models, pass the class directly or via model_json_schema()."],"tags":["azure-ai-inference","structured-output","json-schema","validation","python"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}