Skip to content

To run functional.py example locally, I need to escape """ in Python docstrings #520

@simonff

Description

@simonff

Somehow, https://github.com/stanfordnlp/dspy/blob/main/examples/functional/functional.ipynb works for me on github, but when I try to run the

predictor = TypedPredictor(CodeSignature)
prediction = predictor(
    prompt=PythonCode(code=ds['test'][0]['prompt']),
    test=PythonCode(code=ds['test'][0]['test']),
    entry_point=ds['test'][0]['entry_point']
)

print(prediction)

section in a regular Python script, I keep getting JSON decoding errors.

To fix them, I had to change in dspy/functional/functional.py the return value of _unwrap_json():
return output.replace('"""', '\"\"\"')

Otherwise the """ in the generated code confuse JSON parser (you can replicate by just using json.loads() on the string below):

'{"code": "from typing import List\\n\\n\\ndef has_close_elements(numbers: List[float], threshold: float) -> bool:\\n    \\"\\"\\" Check if in given list of numbers, are any two numbers closer to each other than\\n    given threshold.\\n    >>> has_close_elements([1.0, 2.0, 3.0], 0.5)\\n    False\\n    >>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3)\\n    True\\n    \\"\\"\\"\\n    for i in range(len(numbers)):\\n        for j in range(i + 1, len(numbers)):\\n            if abs(numbers[i] - numbers[j]) < threshold:\\n                return True\\n    return False"}'

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions