Skip to content

bubuild_python_functions_file produces malformed source code when using @with_requirements with other decorators #7209

@Mary904

Description

@Mary904

What happened?

Describe the bug
When using @with_requirements alongside other decorators (like tenacity.retry), the build_python_functions_file utility generates a broken Python file.

To Reproduce
Run the following script:

from typing import Annotated
from tenacity import retry, stop_after_attempt, wait_exponential
import httpx as ht 
from autogen_core.code_executor import with_requirements, FunctionWithRequirements, Alias
from autogen_core.code_executor._func_with_reqs import build_python_functions_file

@with_requirements(
    python_packages=["httpx", "tenacity"], 
    global_imports=[Alias("httpx", "ht")] 
)
@retry(
    stop=stop_after_attempt(3),
    wait=wait_exponential(multiplier=1, min=2, max=10)
)
def fetch_url_content(
    url: Annotated[str, "The URL to fetch"]
) -> str:
    with ht.Client(timeout=10.0) as client:
        response = client.get(url)
        response.raise_for_status()
        return response.text

functions_module = build_python_functions_file([fetch_url_content])
print(functions_module)

Actual Behavior

import httpx as ht

    python_packages=["httpx", "tenacity"], 
    global_imports=[Alias("httpx", "ht")] 
)
@retry(
    stop=stop_after_attempt(3),
    wait=wait_exponential(multiplier=1, min=2, max=10)
)
def fetch_url_content(
    url: Annotated[str, "The URL to fetch"]
) -> str:
    ...

Expected behavior
The expected output should be

import httpx as ht
import tenacity

@retry(
    stop=stop_after_attempt(3),
    wait=wait_exponential(multiplier=1, min=2, max=10)
)
def fetch_url_content(
    url: Annotated[str, "The URL to fetch"]
) -> str:
    ...

Additional context
Add any other context about the problem here.

Which packages was the bug in?

Python Core (autogen-core)

AutoGen library version.

Python 0.7.5

Other library version.

No response

Model used

No response

Model provider

None

Other model provider

No response

Python version

3.12

.NET version

None

Operating system

None

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions