Repository navigation
Conversation
attr, latency and chunk_text were only bound when there was no active role with prettify_roles enabled, but the rendering code below still used them. The first TextOutput inside a role raised UnboundLocalError, and later ones re-rendered the previous chunk's text. Bind them unconditionally so every output renders its own value.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
trace_node_to_html(node, prettify_roles=True)raisedUnboundLocalErroron the first text/token output inside a role, and for later outputs it re-rendered the previous chunk's text instead of the current one, so role content was dropped or duplicated. This PR makes the function render everyTextOutput/TokenOutputinside roles, wrapped in the role styling that the same function already emits.Why
In
guidance/visual/_trace.py,trace_node_to_htmlassignedattr,latencyandchunk_textonly insideif not (active_role and prettify_roles):(guidance/visual/_trace.py:57-60 onmain), while the span-building code directly below reads those variables unconditionally. Withprettify_roles=Trueand an open role:if not isinstance(attr, TokenOutput):withattrunbound, raisingUnboundLocalError: cannot access local variable 'attr' where it is not associated with a value;attr/chunk_text, so the previous chunk's text was emitted again and the current chunk's text never appeared.prettify_rolesis documented as a formatting-only flag ("Enables prettier formatting for roles"), andprettify_roles=Falserenders role text correctly, soprettify_roles=Trueshould render the same text inside the role<div>that this function opens for the role.Changes
guidance/visual/_trace.py: assignattr,latencyandchunk_textfromoutput_attrunconditionally instead of only when there is no active role withprettify_rolesenabled. No other behavior changed.tests/unit/test_visual.py: two regression tests drivingTraceHandlerdirectly (no LLM needed): the first text inside a role, and role text following an earlier non-role chunk (asserting both that the role text is rendered and that the earlier chunk is not duplicated).Testing
On
main(before this change):(the first fails with
UnboundLocalError: cannot access local variable 'attr' where it is not associated with a value; the second because the role text is missing and the previous chunk is rendered twice)With this change:
mypy guidance(informational job in CI) reports the same pre-existing errors asmain; no new errors from this change.Fixes #1522