DocumentationAgent
What it is
- A small helper that uses a LangChain OpenAI chat model to generate markdown documentation for a given Python source file.
- Reads source code and (optionally) existing docs, prompts the model, normalizes the model output, and can write the result to disk.
Public API
class DocumentationAgent(model: str = "gpt-5.2")- Initializes an agent backed by
langchain_openai.ChatOpenAIwithtemperature=0.
- Initializes an agent backed by
DocumentationAgent.model_name -> str(property)- Returns the configured model name.
DocumentationAgent.read_code_file(source_file: Path) -> str- Reads Python source code from
source_fileas UTF-8 text.
- Reads Python source code from
DocumentationAgent.read_existing_documentation(target_file: Path) -> str | None- Reads existing documentation from
target_fileif it exists; otherwise returnsNone.
- Reads existing documentation from
DocumentationAgent.write_documentation_file(target_file: Path, markdown: str) -> None- Creates parent directories as needed and writes
markdowntotarget_fileas UTF-8.
- Creates parent directories as needed and writes
DocumentationAgent.generate_markdown(source_file: Path, target_file: Path) -> str- Builds a prompt using the source code and any existing docs at
target_file, invokes the chat model, and returns normalized markdown (always ends with\n).
- Builds a prompt using the source code and any existing docs at
DocumentationAgent.generate_and_write(source_file: Path, target_file: Path) -> str- Runs
generate_markdown(...), writes the result totarget_file, and returns the markdown.
- Runs
Configuration/Dependencies
- Dependency:
langchain_openai.ChatOpenAI- The agent constructs
ChatOpenAI(model=model, temperature=0).
- The agent constructs
- File I/O uses
pathlib.Pathand assumes UTF-8 encoding.
Usage
from pathlib import Path
from naas_abi_cli.docs_builder.documentation_agent import DocumentationAgent
agent = DocumentationAgent(model="gpt-5.2")
source = Path("path/to/module.py")
target = Path("docs/module.md")
markdown = agent.generate_and_write(source, target)
print(markdown)
Caveats
- Requires access/configuration for
ChatOpenAI(e.g., environment/provider setup); failures will surface when invoking the model. _normalize_markdownstrips a single outer fenced code block (markdown/md/```), if the model returns one.