Zineb Bendhiba created CAMEL-23697:
--------------------------------------
Summary: camel-langchain4j-agent - support Java class as response
format for structured outputs
Key: CAMEL-23697
URL: https://issues.apache.org/jira/browse/CAMEL-23697
Project: Camel
Issue Type: Improvement
Reporter: Zineb Bendhiba
Assignee: Zineb Bendhiba
Following [CAMEL-23642|https://issues.apache.org/jira/browse/CAMEL-23642] which
introduced the jsonSchema endpoint property for structured outputs, this issue
proposes adding a complementary responseType property that accepts a Java class
directly.
+*Motivation*+
jsonSchema requires users to write and maintain a JSON schema file or inline
string. When the structured output maps directly to an existing Java class, it
is more ergonomic to point to that class and let Camel derive the schema
automatically.
+*Proposed API*+
A new responseType URI parameter on camel-langchain4j-agent:
{code:java}
from("direct:extract")
.to("langchain4j-agent:myAgent"
+ "?agentConfiguration=#myAgentConfig"
+ "&responseType=#Person"); // registry reference to
Class<Person>{code}
When responseType is set:
- Camel uses JsonSchemaElementUtils.jsonSchemaElementFrom(Class<?>)
(LangChain4j internal utility) to derive the JSON schema from the class fields
and any @Description annotations.
- The derived schema is injected into the model request via
chatRequestTransformer (same mechanism as jsonSchema).
- The JSON response from the model is automatically unmarshalled to the
target class; the exchange body is the Java object, not a raw JSON string.
+*Constraints*+
- Only works in agentConfiguration mode (same restriction as jsonSchema).
- jsonSchema and responseType are mutually exclusive; setting both throws an
exception at endpoint startup.
+*Implementation notes*+
- JsonSchemaElementUtils is in dev.langchain4j.internal — not a public API
contract, but stable and already used by LangChain4j's own AiServices. Worth
noting in the PR.
- The String return type on
AiAgentWithoutMemoryService/AiAgentWithMemoryService is not a problem: the
schema is applied at the model request level, LangChain4j passes the JSON
through as a String, and Camel unmarshals it with Jackson in the producer.
- No new dependencies required.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)