- How to convert a Python function into a LangChain tool using the
@tooldecorator. - How to inspect generated tool metadata (name, description, args).
- How to call the tool, use its output as context in a prompt, and run a prompt → LLM → output-parser chain.
Notes on what’s happening
- Decorating the function with
@toolconverts it into a StructuredTool-like object that LangChain can inspect and call. The decorator exposes metadata such asname,description, andargs(the argument schema). GetFlightStatus.run(flight)executes the function and returns the static context string shown above. In production you would call a live flight-status API inside this function and return the real response.- The
PromptTemplateuses the tool output ascontext. The chain (prompt | llm | output_parser) takes the populated prompt, sends it to the LLM, and then parses the output into a simple string usingStrOutputParser. - This follows a retrieve-and-read pattern where the retrieval step is replaced by a tool call that supplies up-to-date context to the LLM.
This example uses a static response to keep the demonstration simple. For production, replace the static return with a real API call (include robust error handling, retries, and rate limiting). Also ensure the tool returns well-structured, documented data that your prompt and output parser expect.
- Replace the static
GetFlightStatusimplementation with a real flight-status API to return live information. - Build additional tools (e.g., airport info, weather) and explore creating an agent that selects between them to fulfill more complex user requests.
- Read more about LangChain tools and agents: