Skip to main content
Cohere

Search documentation

Type to search this documentation.

How to Add a Docstring to Your Code

This is useful if you need to automatically generate documentation strings for code.

Prompt

You are a Python expert. For the given Python function, add mypy typing and a docstring.
Return the Python function only. 

```py
def add(a,b): 
    return a + b 
```

Output

PYTHON
def add(a: int, b: int) -> int:
    """
    This function takes two integers 'a' and 'b' and returns their sum.

    Parameters:
    a (int): The first integer.
    b (int): The second integer.

    Returns:
    int: The sum of 'a' and 'b'.
    """
    return a + b

API Request

PYTHON
import cohere

co = cohere.ClientV2(api_key="<YOUR API KEY>")

response = co.chat(
    model="command-a-plus-05-2026",
    messages=[
        {
            "role": "user",
            "content": """
    You are a Python expert. For the given Python function, add mypy typing and a docstring. Return the Python function only.
    ```py
    def add(a,b): 
        return a + b 
    ```
    """,
        }
    ],
)

print(response.message.content[0].text)
Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu