Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

LangChain Structured Output in NestJS: Typed with Zod

说明与用途

在 NestJS 与 LangChain 和 Zod 中,代理返回一个类型化的 OrderDraft 对象,而不是原始文本,简化了订单处理。

问题

Obtaining structured, typed output from a LangChain agent in a NestJS backend instead of raw text.

结果

The agent now returns a typed OrderDraft object, simplifying backend logic and improving reliability.

工作流

  • Define a Zod schema OrderDraft with sku (string) and quantity (positive integer).
  • Import OrderDraft into the OrdersController.
  • Call agent.run() with the customer message and pass {output: OrderDraft} to enforce structured output.
  • Return the output object from the controller method.
  • Add OrdersController to AppModule controllers.
  • Test the endpoint with curl to verify JSON response.
  • Optionally run tests with LangChain fakeModel() to validate without API key.

证据

To get an object instead of text from a LangChain agent in NestJS, pass a Zod schema as output to run().

来源

Same message, same model. Without a schema, the agent answers in text, and your code has to dig the SKU out of a sentence. With one, it answers an object, checked against the schema and typed by it.

来源

  • 记录日期: 2026-11-02T04:43:44.687Z