|
17 | 17 | from google.adk.events.event import Event |
18 | 18 | from google.adk.events.event import NodeInfo |
19 | 19 | from google.adk.events.request_input import RequestInput |
| 20 | +from google.adk.workflow._base_node import BaseNode |
20 | 21 | from google.adk.workflow.utils._rehydration_utils import _ChildScanState |
| 22 | +from google.adk.workflow.utils._rehydration_utils import _process_rehydrated_output |
21 | 23 | from google.adk.workflow.utils._rehydration_utils import _reconstruct_node_states |
22 | 24 | from google.adk.workflow.utils._rehydration_utils import _unwrap_response |
23 | 25 | from google.adk.workflow.utils._rehydration_utils import _validate_resume_response |
@@ -103,6 +105,84 @@ def test_roundtrip_wrap_unwrap_dict(self): |
103 | 105 | assert _unwrap_response(_wrap_response(d)) == d |
104 | 106 |
|
105 | 107 |
|
| 108 | +# --- _process_rehydrated_output --- |
| 109 | + |
| 110 | + |
| 111 | +class TestProcessRehydratedOutput: |
| 112 | + |
| 113 | + def test_extracts_plain_text_without_schema(self): |
| 114 | + node = BaseNode(name="dummy") |
| 115 | + content = types.Content(parts=[types.Part(text="hello world")]) |
| 116 | + assert _process_rehydrated_output(node, content) == "hello world" |
| 117 | + |
| 118 | + def test_returns_plain_text_even_if_json_when_no_schema(self): |
| 119 | + node = BaseNode(name="dummy") |
| 120 | + content = types.Content(parts=[types.Part(text='{"foo": "bar"}')]) |
| 121 | + assert _process_rehydrated_output(node, content) == '{"foo": "bar"}' |
| 122 | + |
| 123 | + def test_parses_json_text_with_output_schema(self): |
| 124 | + class MySchema(BaseModel): |
| 125 | + foo: str |
| 126 | + |
| 127 | + node = BaseNode(name="dummy", output_schema=MySchema) |
| 128 | + content = types.Content(parts=[types.Part(text='{"foo": "bar"}')]) |
| 129 | + assert _process_rehydrated_output(node, content) == {"foo": "bar"} |
| 130 | + |
| 131 | + def test_joins_multiple_parts(self): |
| 132 | + node = BaseNode(name="dummy") |
| 133 | + content = types.Content( |
| 134 | + parts=[types.Part(text="hello "), types.Part(text="world")] |
| 135 | + ) |
| 136 | + assert _process_rehydrated_output(node, content) == "hello world" |
| 137 | + |
| 138 | + def test_filters_thought_parts(self): |
| 139 | + class MySchema(BaseModel): |
| 140 | + answer: int |
| 141 | + |
| 142 | + node = BaseNode(name="dummy", output_schema=MySchema) |
| 143 | + content = types.Content( |
| 144 | + parts=[ |
| 145 | + types.Part(text="thinking...", thought=True), |
| 146 | + types.Part(text='{"answer": 42}'), |
| 147 | + ] |
| 148 | + ) |
| 149 | + assert _process_rehydrated_output(node, content) == {"answer": 42} |
| 150 | + |
| 151 | + def test_returns_none_for_empty_text(self): |
| 152 | + node = BaseNode(name="dummy") |
| 153 | + content = types.Content(parts=[types.Part(text=" ")]) |
| 154 | + assert _process_rehydrated_output(node, content) is None |
| 155 | + |
| 156 | + def test_gracefully_falls_back_on_schema_mismatch(self, caplog): |
| 157 | + class MySchema(BaseModel): |
| 158 | + foo: str |
| 159 | + bar: int # Required field that is missing in the stored output |
| 160 | + |
| 161 | + node = BaseNode(name="dummy", output_schema=MySchema) |
| 162 | + content = types.Content(parts=[types.Part(text='{"foo": "only"}')]) |
| 163 | + |
| 164 | + # Should NOT raise ValueError, but fallback to unvalidated parsed dict |
| 165 | + res = _process_rehydrated_output(node, content) |
| 166 | + assert res == {"foo": "only"} |
| 167 | + assert ( |
| 168 | + "Validation failed for rehydrated output against schema" in caplog.text |
| 169 | + ) |
| 170 | + |
| 171 | + def test_raises_value_error_if_not_valid_json_on_schema_mismatch(self): |
| 172 | + class MySchema(BaseModel): |
| 173 | + foo: str |
| 174 | + |
| 175 | + node = BaseNode(name="dummy", output_schema=MySchema) |
| 176 | + content = types.Content(parts=[types.Part(text="invalid json")]) |
| 177 | + |
| 178 | + # Should raise ValueError because it's not valid JSON |
| 179 | + with pytest.raises( |
| 180 | + ValueError, |
| 181 | + match="Validation failed for rehydrated output against schema", |
| 182 | + ): |
| 183 | + _process_rehydrated_output(node, content) |
| 184 | + |
| 185 | + |
106 | 186 | # --- _validate_resume_response --- |
107 | 187 |
|
108 | 188 |
|
|
0 commit comments