Skip to content

[Fix] Render role text in trace_node_to_html with prettify_roles - #1523

Open
BlueX888 wants to merge 1 commit into
guidance-ai:mainfrom
BlueX888:fix/prep-prettify-roles-unbound-attr
Open

BlueX888 wants to merge 1 commit into
guidance-ai:mainfrom
BlueX888:fix/prep-prettify-roles-unbound-attr

Conversation

@BlueX888

Copy link
Copy Markdown

Summary

trace_node_to_html(node, prettify_roles=True) raised UnboundLocalError on 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 every TextOutput/TokenOutput inside roles, wrapped in the role styling that the same function already emits.

Why

In guidance/visual/_trace.py, trace_node_to_html assigned attr, latency and chunk_text only inside if not (active_role and prettify_roles): (guidance/visual/_trace.py:57-60 on main), while the span-building code directly below reads those variables unconditionally. With prettify_roles=True and an open role:

  • the first output reached if not isinstance(attr, TokenOutput): with attr unbound, raising UnboundLocalError: cannot access local variable 'attr' where it is not associated with a value;
  • subsequent outputs reused the previous chunk's attr/chunk_text, so the previous chunk's text was emitted again and the current chunk's text never appeared.

prettify_roles is documented as a formatting-only flag ("Enables prettier formatting for roles"), and prettify_roles=False renders role text correctly, so prettify_roles=True should render the same text inside the role <div> that this function opens for the role.

Changes

  • guidance/visual/_trace.py: assign attr, latency and chunk_text from output_attr unconditionally instead of only when there is no active role with prettify_roles enabled. No other behavior changed.
  • tests/unit/test_visual.py: two regression tests driving TraceHandler directly (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):

$ python -m pytest tests/unit/test_visual.py -k prettify_roles -v
FAILED tests/unit/test_visual.py::test_trace_node_to_html_prettify_roles_first_text_in_role
FAILED tests/unit/test_visual.py::test_trace_node_to_html_prettify_roles_text_after_text
2 failed, 13 deselected in 0.02s

(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:

$ python -m pytest tests/unit/test_visual.py -k prettify_roles -v
2 passed, 13 deselected in 0.01s

$ python -m pytest tests/unit/test_visual.py -v
15 passed, 1 warning in 0.02s

$ python -m pytest tests/unit/test_trace.py -q
13 passed in 0.02s

$ python -m pytest tests/unit -q
1639 passed, 1 skipped, 220 xfailed, 1 warning in 11.77s

$ ruff format --check
150 files already formatted

$ ruff check --select I,RUF022
All checks passed!

$ ruff check
All checks passed!

mypy guidance (informational job in CI) reports the same pre-existing errors as main; no new errors from this change.

Fixes #1522

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

trace_node_to_html(prettify_roles=True): UnboundLocalError on first text inside a role; later chunks re-render the previous chunk's text

1 participant