azure-cli icon indicating copy to clipboard operation
azure-cli copied to clipboard

Deployment validation fails with 'content already consumed' error

Open madhatter5501 opened this issue 1 year ago β€’ 10 comments

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.

madhatter5501 avatar Sep 20 '25 01:09 madhatter5501

Thank you for opening this issue, we will look into it.

yonzhan avatar Sep 20 '25 01:09 yonzhan

Here are some similar issues that might help you. Please check if they can solve your problem.

  • #31581
  • #31902

Possible solution (Extracted from existing issue, might be incorrect; please verify carefully)

Solution 1:

Downgrade Azure CLI to version 2.72.0, as this version does not have the regression causing the issue. For example, you can use the following commands:

brew uninstall azure-cli
pip3 install azure-cli==2.72.0

This has been confirmed to resolve the issue by multiple users.

Reference:

  • https://github.com/azure/azure-cli/issues/31581#issuecomment-2944491341
  • https://github.com/azure/azure-cli/issues/31581#issuecomment-2944513670
  • https://github.com/azure/azure-cli/issues/31581#issuecomment-2960259244
  • https://github.com/azure/azure-cli/issues/31581#issuecomment-2981256832
  • https://github.com/azure/azure-cli/issues/31581#issuecomment-3012486507
  • https://github.com/azure/azure-cli/issues/31581#issuecomment-3044222220
  • https://github.com/azure/azure-cli/issues/31581#issuecomment-3119472345

Solution 2:

Run the Azure CLI command with the --debug flag to get more detailed output, which can help identify the underlying issue. For example:

az deployment group validate --resource-group test-rg --template-file main.bicep --parameters location=eastus --debug

This approach provides enough information to debug the issue, though it adds noise to the logs.

Reference:

  • https://github.com/azure/azure-cli/issues/31581#issuecomment-2942916860
  • https://github.com/azure/azure-cli/issues/31581#issuecomment-3167244739
  • https://github.com/azure/azure-cli/issues/31581#issuecomment-3160788835
  • https://github.com/azure/azure-cli/issues/31581#issuecomment-3167057366

Solution 3:

Use the what-if command instead of validate or create to check for deployment errors. This command provides more verbose error messages, allowing you to identify and fix issues before deployment. For example:

az deployment group what-if --resource-group testrg --name rollout01 --template-file main.bicep --parameters params.json

Reference:

  • https://github.com/azure/azure-cli/issues/31581#issuecomment-2941917488
  • https://github.com/azure/azure-cli/issues/31581#issuecomment-2988259302
  • https://github.com/azure/azure-cli/issues/31581#issuecomment-2984984267

Powered by issue-sentinel

github-actions[bot] avatar Sep 20 '25 01:09 github-actions[bot]

@yonzhan there was already a PR supposedly fixing it, when is it being released?

till avatar Sep 23 '25 05:09 till

This issue is a direct duplication of #31581, that issue should still be open, it is not proven to be resolved yet.

aglasson avatar Oct 04 '25 10:10 aglasson

πŸš€ Deploying infrastructure for environment: dev ERROR: The content for this response was already consumed INFO: Command ran in 22.134 seconds (init: 0.154, invoke: 21.979) ❌ Deployment failed with exit code: 1 Fetching deployment operations... ERROR: (DeploymentNotFound) Deployment 'aibase-dev-5405' could not be found. Code: DeploymentNotFound Message: Deployment 'dev-5405' could not be found.

this is soooooo bad guys, this issue exsit like several monts. whata is ETA for fixing?

KSemenenko avatar Oct 08 '25 14:10 KSemenenko

What is the current status? this is a critical bug

grogalus avatar Oct 09 '25 16:10 grogalus

I meet the same error. ERROR: The content for this response was already consumed

dongyanme avatar Oct 13 '25 19:10 dongyanme

Thanks for the workaround! πŸ™ Any update on when this is planned to be fixed?

Dzoukr avatar Oct 16 '25 09:10 Dzoukr

I haven't tested the exact issue with the validate command but this same error and scenario occuring for normal deployments has been resolved in 2.78.0. As per related: #31581

aglasson avatar Oct 18 '25 00:10 aglasson

Thanks for your patience here, and sorry for the disruption this caused β€” it was clearly painful in CI.

@madhatter5501 β€” your root-cause was right: the error path in core/commands/arm.py was re-reading an already-consumed HTTP response, which masked the real error. And thanks @aglasson for pinning it as a duplicate of #31581 and flagging the 2.78.0 fix, and @till for chasing the PR.

This is the same regression as #31581, and the fix shipped in CLI 2.78.0. I verified on 2.85.0 that the real error now surfaces β€” for example, validating against a missing resource group returns:

{"code": "ResourceGroupNotFound", "message": "Resource group '<name>' could not be found."}

…instead of "content for this response was already consumed". Both validate and what-if now show the underlying ARM error.

Could you upgrade to the latest CLI (β‰₯ 2.78) and confirm validation shows real errors again? I'll close this as resolved given the fix in 2.78.0 β€” but please reopen with the command + az version if you still hit it on a current CLI.

(The same "content already consumed" masking was also reported on the Bicep side in Azure/bicep#18960, now closed.)

torreymicrosoft avatar Jun 09 '26 21:06 torreymicrosoft