Skip to content

Deployment validation fails with 'content already consumed' errorΒ #32149

@madhatter5501

Description

@madhatter5501

Environment

  • Azure CLI Version: 2.77.0
  • OS: Linux (Ubuntu 22.04)
  • Python: 3.13.7

Describe the bug
Azure CLI deployment validation commands consistently fail with "The content for this response was already consumed" error, completely masking the actual validation errors and making it impossible to validate Bicep templates before deployment.

To Reproduce

  1. Create any Bicep template (valid or invalid)
  2. Run validation command:
    az deployment group validate --resource-group test-rg --template-file main.bicep --parameters location=eastus
  3. Observe the error instead of actual validation results

Expected behavior
The command should either:

  • Successfully validate the template and show validation results, OR
  • Display the actual validation error (e.g., "Resource group 'test-rg' not found")

Actual behavior
Always returns:

The content for this response was already consumed
RuntimeError: The content for this response was already consumed

Stack trace

Traceback (most recent call last):
  File "/opt/az/lib/python3.13/site-packages/requests/models.py", line 897, in content
    raise RuntimeError("The content for this response was already consumed")
RuntimeError: The content for this response was already consumed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/az/lib/python3.13/site-packages/azure/cli/core/commands/arm.py", line 112, in handle_template_based_exception
    raise_subdivision_deployment_error(ex.response.internal_response.text, ex.error.code if ex.error else None)
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/az/lib/python3.13/site-packages/requests/models.py", line 926, in text
    if not self.content:
           ^^^^^^^^^^^^
  File "/opt/az/lib/python3.13/site-packages/requests/models.py", line 897, in content
    raise RuntimeError("The content for this response was already consumed")

Root cause analysis
The issue occurs in the error handling code path in azure/cli/core/commands/arm.py:

  1. A legitimate error occurs (e.g., ResourceGroupNotFound)
  2. Azure CLI attempts to read the HTTP response content to format the error message
  3. The response content has already been consumed by the Azure SDK
  4. The second attempt to read content fails with "content already consumed"
  5. The original error is completely masked

Impact

  • Critical: Prevents proper template validation workflows
  • Masks real errors: Users cannot see actual validation errors
  • Blocks CI/CD: Automated validation scripts fail
  • Affects multiple commands: Both validate and what-if are broken

Workaround
Use local validation tools instead:

# Syntax validation
az bicep build --file template.bicep --stdout > /dev/null

# Best practices check
az bicep lint --file template.bicep

Additional context
This appears to be a regression in error handling where HTTP response content is being consumed multiple times. The fix should ensure response content is not read more than once, or clone the response before initial consumption.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Auto-AssignAuto assign by botAzure Deploymentsaz deployment/bicep/stack/deployment-scripts/ts/group exportPossible-SolutionService AttentionThis issue is responsible by Azure service team.Similar-Issuecustomer-reportedIssues that are reported by GitHub users external to the Azure organization.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions