Skip to content

fix(http-client-python): construct paging JSON body outside prepare_request#10957

Open
l0lawrence wants to merge 1 commit into
microsoft:mainfrom
l0lawrence:l0lawrence/fix-unset-body-placement
Open

fix(http-client-python): construct paging JSON body outside prepare_request#10957
l0lawrence wants to merge 1 commit into
microsoft:mainfrom
l0lawrence:l0lawrence/fix-unset-body-placement

Conversation

@l0lawrence

@l0lawrence l0lawrence commented Jun 10, 2026

Copy link
Copy Markdown
Member

Bug

For paging operations whose request body is a flattened JSON model, the generated client raised UnboundLocalError: cannot access local variable 'body' on the first call. The body construction block was emitted inside the prepare_request closure, and since body is assigned there, Python treats it as a function-local for the whole closure, so the earlier if body is _Unset: read fails before it is ever bound.

Fix

Construct the JSON model body once in the enclosing method scope (next to where overload body initialization already lives) and before the body is serialized into the request content. The prepare_request closure then only reads the already-built body. This mirrors the existing pattern where _initialize_overloads returns early for paging so body setup happens once outside the closure.

…equest

For paging operations with a flattened JSON model body, the body construction (if body is _Unset: ...) was emitted inside the prepare_request closure. Assigning �ody there made it a function-local, so reading if body is _Unset: raised UnboundLocalError on every page fetch. Move the construction out of the closure and before the body is serialized into the request content.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:client:python Issue for the Python client emitter: @typespec/http-client-python label Jun 10, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jun 10, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-python@10957

commit: 2107619

@github-actions

Copy link
Copy Markdown
Contributor

All changed packages have been documented.

  • @typespec/http-client-python
Show changes

@typespec/http-client-python - fix ✏️

Fix UnboundLocalError for paging operations with a flattened JSON model body. The request body is now constructed once outside the prepare_request callback (and before the body is serialized into the request content) instead of inside the closure, where assigning body made it an unbound local on every page fetch.

@azure-sdk-automation

azure-sdk-automation Bot commented Jun 10, 2026

Copy link
Copy Markdown

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

@msyyc

msyyc commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

We don't have spector case for this scenario for now so better to add unit test cases instead @l0lawrence

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:python Issue for the Python client emitter: @typespec/http-client-python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants