Repository navigation
fix(python): community fixes for the openai, cartesia and pipecat wrappers #1799
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
Merged
+1,914
−126
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
packages/cartesia-sdk-python/tests/test_single_agent_run.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| """Regression tests: the wrapped agent must run exactly once per event.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import sys | ||
| import types | ||
| import unittest | ||
| from types import SimpleNamespace | ||
| from unittest.mock import AsyncMock | ||
|
|
||
|
|
||
| def _install_test_stubs() -> None: | ||
| if "loguru" not in sys.modules: | ||
| loguru_module = types.ModuleType("loguru") | ||
|
|
||
| class _Logger: | ||
| def info(self, *_args, **_kwargs): | ||
| return None | ||
|
|
||
| def debug(self, *_args, **_kwargs): | ||
| return None | ||
|
|
||
| def warning(self, *_args, **_kwargs): | ||
| return None | ||
|
|
||
| def error(self, *_args, **_kwargs): | ||
| return None | ||
|
|
||
| loguru_module.logger = _Logger() | ||
| sys.modules["loguru"] = loguru_module | ||
|
|
||
| if "pydantic" not in sys.modules: | ||
| pydantic_module = types.ModuleType("pydantic") | ||
|
|
||
| class BaseModel: | ||
| def __init__(self, **kwargs): | ||
| for key, value in kwargs.items(): | ||
| setattr(self, key, value) | ||
|
|
||
| def Field(*, default=None, **_kwargs): | ||
| return default | ||
|
|
||
| pydantic_module.BaseModel = BaseModel | ||
| pydantic_module.Field = Field | ||
| sys.modules["pydantic"] = pydantic_module | ||
|
|
||
|
|
||
| _install_test_stubs() | ||
|
|
||
| from supermemory_cartesia.agent import SupermemoryCartesiaAgent | ||
|
|
||
|
|
||
| class _StreamingAgent: | ||
| """Inner agent that yields chunks; optionally dies mid-stream.""" | ||
|
|
||
| def __init__(self, fail_after_chunks: bool = False): | ||
| self.fail_after_chunks = fail_after_chunks | ||
| self.run_count = 0 | ||
|
|
||
| async def process(self, env, event): | ||
| self.run_count += 1 | ||
| yield "chunk-1" | ||
| yield "chunk-2" | ||
| if self.fail_after_chunks: | ||
| raise RuntimeError("stream dropped") | ||
|
|
||
|
|
||
| def _wrap(inner): | ||
| return SupermemoryCartesiaAgent( | ||
| agent=inner, | ||
| api_key="mock_key", | ||
| container_tag="user-123", | ||
| custom_id="conversation-456", | ||
| ) | ||
|
|
||
|
|
||
| def _user_turn_ended_event(): | ||
| return type("UserTurnEnded", (), {})() | ||
|
|
||
|
|
||
| class TestSingleAgentRun(unittest.IsolatedAsyncioTestCase): | ||
| async def test_mid_stream_agent_error_is_not_retried(self) -> None: | ||
| inner = _StreamingAgent(fail_after_chunks=True) | ||
| wrapper = _wrap(inner) | ||
|
|
||
| outputs = [] | ||
| with self.assertRaises(RuntimeError): | ||
| async for out in wrapper.process(None, SimpleNamespace()): | ||
| outputs.append(out) | ||
|
|
||
| self.assertEqual(outputs, ["chunk-1", "chunk-2"]) | ||
| self.assertEqual(inner.run_count, 1) | ||
|
|
||
| async def test_mid_stream_error_on_user_turn_is_not_retried(self) -> None: | ||
| inner = _StreamingAgent(fail_after_chunks=True) | ||
| wrapper = _wrap(inner) | ||
| wrapper._enrich_event_with_memories = AsyncMock( | ||
| side_effect=lambda event: (event, None) | ||
| ) | ||
|
|
||
| outputs = [] | ||
| with self.assertRaises(RuntimeError): | ||
| async for out in wrapper.process(None, _user_turn_ended_event()): | ||
| outputs.append(out) | ||
|
|
||
| self.assertEqual(outputs, ["chunk-1", "chunk-2"]) | ||
| self.assertEqual(inner.run_count, 1) | ||
|
|
||
| async def test_enrichment_failure_still_runs_agent_once(self) -> None: | ||
| inner = _StreamingAgent() | ||
| wrapper = _wrap(inner) | ||
| wrapper._enrich_event_with_memories = AsyncMock( | ||
| side_effect=RuntimeError("enrichment exploded") | ||
| ) | ||
|
|
||
| outputs = [ | ||
| out async for out in wrapper.process(None, _user_turn_ended_event()) | ||
| ] | ||
|
|
||
| self.assertEqual(outputs, ["chunk-1", "chunk-2"]) | ||
| self.assertEqual(inner.run_count, 1) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.