Skip to content

utils: keep docstring continuation lines that contain a colon - #747

Open
breken-ai wants to merge 1 commit into
ollama:mainfrom
breken-ai:fix/docstring-continuation-colon
Open

breken-ai wants to merge 1 commit into
ollama:mainfrom
breken-ai:fix/docstring-continuation-colon

Conversation

@breken-ai

Copy link
Copy Markdown

When a function is passed as a tool, a wrapped argument description is cut short if a continuation line contains a colon or a parenthesis:

def get_page(url: str, date: str) -> str:
    """Fetch a page.

    Args:
        url: Page to fetch, for example
            https://example.com/docs
        date: Day to fetch, in the format
            YYYY-MM-DD (for example: 2024-01-31)
    """

The schema that chat(tools=[get_page]) sends today:

{"url":  {"type": "string", "description": "Page to fetch, for example"},
 "date": {"type": "string", "description": "Day to fetch, in the format"}}

_parse_docstring strips every line in the Args: section and then treats any line with a : (or a (...)) as a new argument. The continuation lines https://example.com/docs and YYYY-MM-DD (for example: 2024-01-31) become phantom arguments named https and YYYY-MM-DD. The real descriptions stop at the line before, so the model never sees the URL example or the date format. Continuation lines without a colon already work, and test_function_docstring_parsing covers them.

This change keeps each line's indentation for the Args: section. A line indented deeper than the current argument's line is treated as a continuation, even if it contains a colon, which is how Google-style docstrings wrap descriptions. Lines at the argument's own indentation are parsed as before, so the existing parenthesis and skewed-docstring cases don't change.

Verification

  • New test_docstring_continuation_lines_with_colons fails on main ('Page to fetch, for example') and passes with this change.
  • End to end through Client.chat(tools=[get_page]) with an httpx.MockTransport: the request now carries "Page to fetch, for example https://example.com/docs" and "Day to fetch, in the format YYYY-MM-DD (for example: 2024-01-31)".
  • pytest ollama tests passes (103 tests), and ruff format --check and ruff check are clean.

Built by breken, your AI support engineer - breken.ai - this one's on us.

_parse_docstring treated every line in the Args section that contains a
colon or a parenthesis as a new argument. An indented continuation line
such as a URL or "YYYY-MM-DD (for example: 2024-01-31)" therefore ended
the real argument's description early and created a phantom argument,
so the tool schema sent to the model lost that part of the description.

Treat lines indented deeper than the current argument as continuation
lines, which is how Google-style docstrings wrap descriptions.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant