Deployment validation fails with 'content already consumed' error
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
- Create any Bicep template (valid or invalid)
- Run validation command:
az deployment group validate --resource-group test-rg --template-file main.bicep --parameters location=eastus - 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:
- A legitimate error occurs (e.g., ResourceGroupNotFound)
- Azure CLI attempts to read the HTTP response content to format the error message
- The response content has already been consumed by the Azure SDK
- The second attempt to read content fails with "content already consumed"
- 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
validateandwhat-ifare 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.
Thank you for opening this issue, we will look into it.
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
@yonzhan there was already a PR supposedly fixing it, when is it being released?
This issue is a direct duplication of #31581, that issue should still be open, it is not proven to be resolved yet.
π 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?
What is the current status? this is a critical bug
I meet the same error. ERROR: The content for this response was already consumed
Thanks for the workaround! π Any update on when this is planned to be fixed?
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
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.)