Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Would like have the ability to fix json output when streaming from a structure #1649

Open
shhlife opened this issue Feb 5, 2025 · 1 comment
Assignees
Labels
Milestone

Comments

@shhlife
Copy link

shhlife commented Feb 5, 2025

When streaming output from a structure that's using structured output, it would be helpful to ensure the output data is valid.

For example:

from griptape.structures import Agent
from griptape.utils import Stream
import schema
agent = Agent(stream=True, 
              output_schema=schema.Schema({"item": str, "description": str})
              )
full_result = ""
for artifact in Stream(agent).run("give me a cookie name and its description"):
    full_result += artifact.value
    print(full_result)

will print out partial strings as the chunks come back

{"
{"item
{"item":"
{"item":"**
{"item":"**C
{"item":"**Cinnamon
{"item":"**Cinnamon Sw
{"item":"**Cinnamon Swirl
{"item":"**Cinnamon Swirl Delight
{"item":"**Cinnamon Swirl Delight**
{"item":"**Cinnamon Swirl Delight**","
{"item":"**Cinnamon Swirl Delight**","description
{"item":"**Cinnamon Swirl Delight**","description":"
{"item":"**Cinnamon Swirl Delight**","description":"A
{"item":"**Cinnamon Swirl Delight**","description":"A delightful

it's cool seeing this, but unfortunately the results aren't clean json.

If we integrate the library json-repair, we can make it output cleaner results..

from griptape.structures import Agent
from griptape.utils import Stream
from json_repair import repair_json # json_repair
import schema
agent = Agent(stream=True, 
              output_schema=schema.Schema({"item": str, "description": str})
              )
full_result = ""
for artifact in Stream(agent).run("give me a cookie name and a short description"):
    full_result += artifact.value
    print(repair_json(full_result)) # repair it

and we get these results:

""
{}
{}
{"item": ""}
{"item": "Moon"}
{"item": "Moonlit"}
{"item": "Moonlit Almond"}
{"item": "Moonlit Almond Delight"}
{"item": "Moonlit Almond Delight"}
{"item": "Moonlit Almond Delight"}
{"item": "Moonlit Almond Delight", "description": ""}
{"item": "Moonlit Almond Delight", "description": "A"}

can we add this as part of Stream?

@collindutter
Copy link
Member

Once Loaders are Driver-based, this library would fit great into JsonParser.parse.

@collindutter collindutter modified the milestones: 1.4, 1.5 Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants