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

[Fix] Removed unnecessarily changing tuple output to list output #608

Merged
merged 10 commits into from
Jan 9, 2025

Conversation

zli11010
Copy link
Contributor

📥 Pull Request

📘 Description
Fixed this bug: #607

🧪 Testing
Ran my code that uses AgentOps and now it works. (Changing list to tuple doesn't affect what gets logged.)

Comment on lines -70 to -73
# If the function returns multiple values, record them all in the same event
if isinstance(returns, tuple):
returns = list(returns)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of removing these functions, we can instead do what we intended at fist-- just return a tuple

if istinatnce(returns, tuple):
    returns = tuple(returns)

Removing these lines of code altogether will have unintended consequences.

Copy link
Contributor Author

@zli11010 zli11010 Dec 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask what the suggested change does? It seems (in most cases) equivalent to removing the two lines of code.

Unfortunately your suggested change would still break the code I'm using, since the code checks the output of the function is an instance of a specific subclass of tuple (more specifically, a custom namedtuple). Maybe one could argue that this is bad practice, but I believe it's more important that a decorator that does logging doesn't change the output of the function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but AFAIK the only reason the tuple was being cast to a list was so that it can be dumped into a JSON, but json.dumps actually handles that automatically.

Indeed that should be handled by a JSONEncoder which is where these sorts of casts belong

@areibman
Copy link
Contributor

Left a review @zli11010-- should be an easy fix! Please let me know if it still causes problems with the repo you're trying

@teocns
Copy link
Contributor

teocns commented Dec 29, 2024

@areibman can we have more documentation around the expected functionality of returns? I too had a hard time figuring out, maybe some docstrings will do 👍

@zli11010
Copy link
Contributor Author

zli11010 commented Dec 29, 2024

@areibman Since you had concerns about deleting the lines, I tried to keep the original semantics with the only change being fixing the fact that the type casting modified the output of the original function. Hopefully that's better!

@the-praxs
Copy link
Member

What's the status of this PR?

@areibman
Copy link
Contributor

areibman commented Jan 4, 2025

@teocns can you take a stab at completing this? Just want to make sure we don’t have any downstream effects if we merge

Copy link

codecov bot commented Jan 4, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Flag Coverage Δ
unittests 37.80% <100.00%> (+0.30%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
agentops/decorators.py 71.34% <100.00%> (+3.06%) ⬆️

... and 2 files with indirect coverage changes

teocns and others added 2 commits January 3, 2025 19:33
Added tests to verify PR AgentOps-AI#608, which fixes decorators changing tuple outputs
into lists. Checks:

1. Decorators keep the original return type for:
   - Simple tuples like (1, "test")
   - Named tuples like Point(x=1, y=2)
   - Nested tuples containing multiple values
   - Custom tuple types (fixing bug AgentOps-AI#607)

2. Tests also show that JSON handles tuples correctly without needing
   to convert them to lists first, proving the point that the
   conversion was unnecessary.
@teocns
Copy link
Contributor

teocns commented Jan 4, 2025

The removal of the tuple-to-list conversion seems to be the right approach; the conversion was actually unnecessary since:

  • Our serialization layer (safe_serialize and filter_unjsonable) already handles tuples correctly
  • The conversion was happening too early (at decorator level) rather than at serialization time

Added some tests to ensure @record_* decorators behave correctly in regards to the issue, especially proving the json serialization utilities already handle tuple types correctly without needing to convert them to lists first, proving the point that the conversion was unnecessary :)

If this gets merged, we should open a ticket to unify:

into:


Rationale: Both record_action and record_tool are decorators with similar functionality and testing patterns.
Having their tests in separate files creates unnecessary fragmentation.

@teocns teocns merged commit ad5a17f into AgentOps-AI:main Jan 9, 2025
1 check passed
@teocns teocns mentioned this pull request Jan 9, 2025
teocns added a commit that referenced this pull request Jan 9, 2025
Fix style in pr #608
Signed-off-by: Teo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants