Skip to content

Map Function strict flag and FunctionParam syntax in ChatReqLLM tools#583

Merged
brainlid merged 1 commit into
mainfrom
me-fix-strict-function-mapping-reqllm
Jul 8, 2026
Merged

Map Function strict flag and FunctionParam syntax in ChatReqLLM tools#583
brainlid merged 1 commit into
mainfrom
me-fix-strict-function-mapping-reqllm

Conversation

@brainlid

@brainlid brainlid commented Jul 8, 2026

Copy link
Copy Markdown
Owner

Problem

ChatReqLLM.function_to_req_llm_tool/1 only understood one of the two ways a LangChain.Function can declare its parameters. It read fun.parameters_schema (the raw JSONSchema map) directly and ignored fun.parameters — the validated LangChain.FunctionParam list form. Any tool defined with FunctionParam structs was handed to the LLM with an empty (or nil) schema. It also dropped fun.strict on the floor, so strict structured-output tool calling never reached the provider through the ReqLLM backend.

Solution

Introduce a private get_parameter_schema/1 in ChatReqLLM that pattern-matches the three parameter cases and produces a correct JSONSchema map in each, mirroring LangChain.ChatModels.ChatOpenAI.get_parameters/1:

  • empty :parameters and nil :parameters_schema%{"type" => "object", "properties" => %{}}
  • empty :parameters with a raw :parameters_schema map → passthrough
  • a :parameters list of FunctionParam structs → FunctionParam.to_parameters_schema/1

The tool builder now calls that helper for parameter_schema and also passes strict: fun.strict through to ReqLLM.Tool.new!/1, so providers that support it (e.g. OpenAI structured outputs) can enforce the schema. This brings the ReqLLM chat model to parity with ChatOpenAI for tool definitions.

Changes

  • lib/chat_models/chat_req_llm.ex — Added get_parameter_schema/1 supporting both :parameters (FunctionParam list) and :parameters_schema (raw map) forms; pass strict: fun.strict through to the ReqLLM tool; alias LangChain.FunctionParam
  • lib/chat_models/chat_req_llm.ex (@doc) — Documented the strict passthrough and dual parameter-form support on function_to_req_llm_tool/1
  • test/chat_models/chat_req_llm_test.exs — Added coverage: strict defaults to false, strict: true passes through, strict serializes in OpenAI schema format, FunctionParam list maps to a schema, and no-params maps to an empty object schema

Testing

Five new unit tests in test/chat_models/chat_req_llm_test.exs cover the strict passthrough (including ReqLLM.Tool.to_schema(:openai) serialization) and both parameter-declaration forms. No live API calls required.

- also implements support for mapping FunctionParam syntax
@brainlid
brainlid merged commit c24ef4d into main Jul 8, 2026
2 checks passed
@brainlid
brainlid deleted the me-fix-strict-function-mapping-reqllm branch July 8, 2026 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant