diff --git a/guidance/_parser.py b/guidance/_parser.py index 600d0cb43..331f2210e 100644 --- a/guidance/_parser.py +++ b/guidance/_parser.py @@ -594,10 +594,14 @@ def _record_captures_from_root(self, initial_item, data, log_prob_data): def _compute_parse_tree(self, initial_pos, initial_item, reversed_state_sets): stack = [(initial_pos, initial_item)] + seen = set() while stack: pos, item = stack.pop() - + if (pos, item) in seen: + # Skip items we have already processed + continue + seen.add((pos, item)) # compute the children for this item assert self._compute_children(pos, item, reversed_state_sets)