For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
DASHBOARDPLAYGROUNDDOCSCOMMUNITYLOG IN
Guides and conceptsAPI ReferenceRelease NotesLLMUCookbooks
Guides and conceptsAPI ReferenceRelease NotesLLMUCookbooks
  • Get Started
    • Introduction
    • Installation
    • Creating a client
    • Playground
    • FAQs
  • Models
    • An Overview of Cohere's Models
    • Aya
    • Embed
    • Rerank
  • Text Generation
    • Introduction to Text Generation at Cohere
    • Using the Chat API
    • Reasoning
    • Image Inputs
    • Streaming Responses
    • Predictable Outputs
    • Advanced Generation Parameters
    • Tool Use
    • Tokens and Tokenizers
        • Add a Docstring to your code
    • Summarizing Text
    • Safety Modes
  • Embeddings (Vectors, Search, Retrieval)
    • Introduction to Embeddings at Cohere
    • Semantic Search with Embeddings
    • Multimodal Embeddings
    • Batch Embedding Jobs
  • Going to Production
    • API Keys and Rate Limits
    • Going Live
    • Deprecations
    • How Does Cohere's Pricing Work?
  • Integrations
    • Integrating Embedding Models with Other Tools
    • Cohere and LangChain
    • LlamaIndex and Cohere
  • Deployment Options
    • Overview
    • SDK Compatibility
  • Tutorials
    • Cookbooks
    • LLM University
    • Build Things with Cohere!
    • Agentic RAG
    • Cohere on Azure
  • Responsible Use
    • Security
    • Usage Policy
    • Command A Technical Report
    • Command R and Command R+ Model Card
  • Cohere Labs
    • Cohere Labs Acceptable Use Policy
  • More Resources
    • Cohere Toolkit
    • Datasets
    • Improve Cohere Docs
LogoLogodocs
DASHBOARDPLAYGROUNDDOCSCOMMUNITYLOG IN
Text GenerationPrompt EngineeringPrompt Library

How to Add a Docstring to Your Code

Was this page helpful?
Edit this page
Previous

Summarizing Text with the Chat Endpoint

Next
Built with

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
1def add(a: int, b: int) -> int:
2 """
3 This function takes two integers 'a' and 'b' and returns their sum.
4
5 Parameters:
6 a (int): The first integer.
7 b (int): The second integer.
8
9 Returns:
10 int: The sum of 'a' and 'b'.
11 """
12 return a + b

API Request

PYTHON
1import cohere
2
3co = cohere.ClientV2(api_key="<YOUR API KEY>")
4
5response = co.chat(
6 model="command-a-plus-05-2026",
7 messages=[
8 {
9 "role": "user",
10 "content": """
11 You are a Python expert. For the given Python function, add mypy typing and a docstring. Return the Python function only.
12 ```py
13 def add(a,b):
14 return a + b
15 ```
16 """,
17 }
18 ],
19)
20
21print(response.message.content[0].text)