guidance icon indicating copy to clipboard operation
guidance copied to clipboard

stack overflow in .serialize() of substring()

Open mmoskal opened this issue 1 year ago • 2 comments

The following:

from guidance import substring
b = ("Hello " + substring("foobar baz " * 100)).serialize()
print(len(b))

Throw stack overflow exception:

Traceback (most recent call last):
  File "/Users/michal/test.py", line 3, in <module>
    b = ("Hello " + substring("foobar baz " * 100)).serialize()
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/michal/src/ai/guidance/guidance/_grammar.py", line 212, in serialize
    self._rec_create_index_map(index_map) # gives all the nodes an index
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/michal/src/ai/guidance/guidance/_grammar.py", line 222, in _rec_create_index_map
    value._rec_create_index_map(index_map)
  File "/Users/michal/src/ai/guidance/guidance/_grammar.py", line 222, in _rec_create_index_map
    value._rec_create_index_map(index_map)
  File "/Users/michal/src/ai/guidance/guidance/_grammar.py", line 222, in _rec_create_index_map
    value._rec_create_index_map(index_map)
  [Previous line repeated 995 more times]
RecursionError: maximum recursion depth exceeded

mmoskal avatar Apr 04 '24 22:04 mmoskal

It looks like you would need an explicit TODO queue/stack in _rec_create_index_map and _rec_serialize

mmoskal avatar Apr 04 '24 22:04 mmoskal

Yeah I think a stack based approach in both the _rec functions is likely to do the trick. I'll play with this. Thanks for the catch Michal :).

Harsha-Nori avatar Apr 13 '24 17:04 Harsha-Nori