MCPcopy Index your code
hub / github.com/TIGER-AI-Lab/TheoremExplainAgent / _extract_code

Function _extract_code

src/utils/utils.py:28–46  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

26 print(f"\n{separator}")
27
28def _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
48def extract_json(response: str) -> dict:
49 """Extract and parse JSON content from a text response.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected