-
Notifications
You must be signed in to change notification settings - Fork 184
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
Add JsonArtifact
#937
Add JsonArtifact
#937
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅ 📢 Thoughts on this report? Let us know! |
Could you throw in a |
griptape/artifacts/json_artifact.py
Outdated
|
||
@property | ||
def value(self) -> Any: | ||
return json.loads(self.value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The codecov warning here seems worth testing.
pyproject.toml
Outdated
@@ -28,6 +28,7 @@ stringcase = "^1.2.0" | |||
docker = "^7.1.0" | |||
sqlalchemy = "~=1.0" | |||
requests = "^2.32.0" | |||
jsonalias = "^0.1.2" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this is even worth including since its usage is so limited. Can you just include the type in JsonArtifact
?
def test_value_type_conversion(self): | ||
assert JsonArtifact({"foo": "bar"}).value == json.loads(json.dumps({"foo": "bar"})) | ||
string = json.dumps({"foo": {}}) | ||
print(string) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove
string = json.dumps({"foo": {}}) | ||
print(string) | ||
string = json.loads(string) | ||
print(string) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove
9782878
to
6c894d9
Compare
6c894d9
to
5a1c7b6
Compare
@@ -58,3 +58,7 @@ A [BooleanArtifact](../../reference/griptape/artifacts/boolean_artifact.md) is u | |||
A [GenericArtifact](../../reference/griptape/artifacts/generic_artifact.md) can be used as an escape hatch for passing any type of data around the framework. | |||
It is generally not recommended to use this Artifact type, but it can be used in a handful of situations where no other Artifact type fits the data being passed. | |||
See [talking to a video](../../examples/talk-to-a-video.md) for an example of using a `GenericArtifact` to pass a Gemini-specific video file. | |||
|
|||
## JsonArtifact |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Header should just be Json
griptape/artifacts/json_artifact.py
Outdated
@define | ||
class JsonArtifact(BaseArtifact): | ||
_obj: Any = field(metadata={"serializable": True}, alias="obj") | ||
value: dict = field(init=False, metadata={"serializable": True}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be value: Json
?
griptape/artifacts/json_artifact.py
Outdated
def __attrs_post_init__(self) -> None: | ||
if self._obj is None: | ||
self._obj = {} | ||
self._json_str = json.dumps(self._obj) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need a field for this? Can we just dumps in to_text
?
griptape/artifacts/json_artifact.py
Outdated
|
||
@define | ||
class JsonArtifact(BaseArtifact): | ||
_obj: Any = field(metadata={"serializable": True}, alias="obj") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this field provide?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is just the original object being converted. different than value
. consistent with json.dumps
signature.
import json | ||
|
||
import pytest | ||
|
||
from griptape.artifacts import JsonArtifact, TextArtifact | ||
|
||
|
||
class TestJsonArtifact: | ||
def test_value_type_conversion(self): | ||
assert JsonArtifact({"foo": "bar"}).value == json.loads(json.dumps({"foo": "bar"})) | ||
|
||
def test___add__(self): | ||
assert (JsonArtifact({"foo": "bar"}) + JsonArtifact({"value": "baz"})).value == JsonArtifact( | ||
{"foo": "bar", "value": "baz"} | ||
).value | ||
|
||
new = JsonArtifact({"foo": "bar"}) + JsonArtifact({"value": "baz"}) | ||
assert new.value == {"foo": "bar", "value": "baz"} | ||
|
||
assert (JsonArtifact({"foo": "bar"}) + JsonArtifact({"foo": "baz"})).value == JsonArtifact({"foo": "baz"}).value | ||
with pytest.raises(ValueError): | ||
JsonArtifact({"foo": "bar"}) + TextArtifact("invalid json") | ||
|
||
def test_to_text(self): | ||
assert JsonArtifact({"foo": "bar"}).to_text() == json.dumps({"foo": "bar"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add tests for all the types that Json
can be? dict[str, "Json"], list["Json"], str, int, float, bool, None
Describe your changes
Issue ticket number and link
closes #935
📚 Documentation preview 📚: https://griptape--937.org.readthedocs.build//937/