MCPcopy Index your code
hub / github.com/openai/openai-agents-python / generate_func_documentation

Function generate_func_documentation

src/agents/function_schema.py:148–187  ·  view source on GitHub ↗

Extracts metadata from a function docstring, in preparation for sending it to an LLM as a tool. Args: func: The function to extract documentation from. style: The style of the docstring to use for parsing. If not provided, we will attempt to auto-detect the styl

(
    func: Callable[..., Any], style: DocstringStyle | None = None
)

Source from the content-addressed store, hash-verified

146
147
148def generate_func_documentation(
149 func: Callable[..., Any], style: DocstringStyle | None = None
150) -> FuncDocumentation:
151 """
152 Extracts metadata from a function docstring, in preparation for sending it to an LLM as a tool.
153
154 Args:
155 func: The function to extract documentation from.
156 style: The style of the docstring to use for parsing. If not provided, we will attempt to
157 auto-detect the style.
158
159 Returns:
160 A FuncDocumentation object containing the function's name, description, and parameter
161 descriptions.
162 """
163 name = func.__name__
164 doc = inspect.getdoc(func)
165 if not doc:
166 return FuncDocumentation(name=name, description=None, param_descriptions=None)
167
168 with _suppress_griffe_logging():
169 docstring = Docstring(doc, lineno=1, parser=style or _detect_docstring_style(doc))
170 parsed = docstring.parse()
171
172 description: str | None = next(
173 (section.value for section in parsed if section.kind == DocstringSectionKind.text), None
174 )
175
176 param_descriptions: dict[str, str] = {
177 param.name: param.description
178 for section in parsed
179 if section.kind == DocstringSectionKind.parameters
180 for param in section.value
181 }
182
183 return FuncDocumentation(
184 name=func.__name__,
185 description=description,
186 param_descriptions=param_descriptions or None,
187 )
188
189
190def _strip_annotated(annotation: Any) -> tuple[Any, tuple[Any, ...]]:

Callers 4

test_auto_detectionFunction · 0.90
test_instance_methodFunction · 0.90
test_classmethodFunction · 0.90
function_schemaFunction · 0.85

Calls 4

FuncDocumentationClass · 0.85
_suppress_griffe_loggingFunction · 0.85
_detect_docstring_styleFunction · 0.85
parseMethod · 0.45

Tested by 3

test_auto_detectionFunction · 0.72
test_instance_methodFunction · 0.72
test_classmethodFunction · 0.72