Extract code blocks from a text response. Extracts Python code blocks delimited by ```python markers. If no code blocks are found, returns the entire response text. Args: response_text (str): The text response containing code blocks Returns: str: The extracted code
(response_text: str)
| 26 | print(f"\n{separator}") |
| 27 | |
| 28 | def _extract_code(response_text: str) -> str: |
| 29 | """Extract code blocks from a text response. |
| 30 | |
| 31 | Extracts Python code blocks delimited by ```python markers. If no code blocks are found, |
| 32 | returns the entire response text. |
| 33 | |
| 34 | Args: |
| 35 | response_text (str): The text response containing code blocks |
| 36 | |
| 37 | Returns: |
| 38 | str: The extracted code blocks joined by newlines, or the full response if no blocks found |
| 39 | """ |
| 40 | code = "" |
| 41 | code_blocks = re.findall(r'```python\n(.*?)\n```', response_text, re.DOTALL) |
| 42 | if code_blocks: |
| 43 | code = "\n\n".join(code_blocks) |
| 44 | elif "```" not in response_text: # if no code block, return the whole response |
| 45 | code = response_text |
| 46 | return code |
| 47 | |
| 48 | def extract_json(response: str) -> dict: |
| 49 | """Extract and parse JSON content from a text response. |
nothing calls this directly
no outgoing calls
no test coverage detected