-
Notifications
You must be signed in to change notification settings - Fork 17
Streaming Converter Implementation for LLM JSONL Processing #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
igordayen
wants to merge
6
commits into
main
Choose a base branch
from
llm-streaming
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f68f7c6
Streaming Jackson LLM Output Converter
igordayen a55a725
Streaming support: enhance prompt
igordayen 4ca99a4
JSONL Streaming Converter enhannced with Filtering Support
igordayen 3637767
Streaming Capability marker interface
igordayen c205699
Generalized / Centralized Thinking detection
igordayen 9d34e65
Streaming Jackson Converter: addressing code review feedback
igordayen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
...main/kotlin/com/embabel/common/ai/converters/streaming/StreamingJacksonOutputConverter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* | ||
| * Copyright 2024-2025 Embabel Software, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.embabel.common.ai.converters.streaming | ||
|
|
||
| import com.embabel.common.ai.converters.FilteringJacksonOutputConverter | ||
| import com.embabel.common.core.streaming.StreamingEvent | ||
| import com.embabel.common.core.streaming.StreamingUtils | ||
| import com.fasterxml.jackson.databind.ObjectMapper | ||
| import org.springframework.core.ParameterizedTypeReference | ||
| import reactor.core.publisher.Flux | ||
| import java.util.function.Predicate | ||
|
|
||
| /** | ||
| * Streaming output converter that extends FilteringJacksonOutputConverter to support JSONL format. | ||
| * | ||
| * This converter enables streaming LLM responses by: | ||
| * - Converting JSONL (JSON Lines) input to reactive Flux<T> streams of typed objects | ||
| * - Supporting mixed content streams with both objects and thinking content via StreamingEvent<T> | ||
| * - Inheriting JSON schema injection and property filtering capabilities from parent FilteringJacksonOutputConverter | ||
| * - Providing instructions to LLMs for proper JSONL output formatting | ||
| * | ||
| * Use cases: | ||
| * - Streaming lists of objects from LLM responses in real-time | ||
| * - Processing LLM reasoning (thinking) alongside structured outputs | ||
| * - Real-time agent progress monitoring and incremental results | ||
| * | ||
| * The converter requests JSONL format from LLMs and parses each line as a separate | ||
| * JSON object, emitting them as reactive stream events as they become available. | ||
| */ | ||
| class StreamingJacksonOutputConverter<T> : FilteringJacksonOutputConverter<T> { | ||
|
|
||
| constructor( | ||
| clazz: Class<T>, | ||
| objectMapper: ObjectMapper, | ||
| propertyFilter: Predicate<String> = Predicate { true }, | ||
| ) : super(clazz, objectMapper, propertyFilter) | ||
|
|
||
| constructor( | ||
| typeReference: ParameterizedTypeReference<T>, | ||
| objectMapper: ObjectMapper, | ||
| propertyFilter: Predicate<String> = Predicate { true }, | ||
| ) : super(typeReference, objectMapper, propertyFilter) | ||
|
|
||
| /** | ||
| * Convert streaming JSONL text to a Flux of typed objects. | ||
| * Each line should be a valid JSON object matching the schema. | ||
| * Uses resilient error handling - logs warnings for null conversions but continues processing other lines. | ||
| */ | ||
| fun convertStream(jsonlContent: String): Flux<T> { | ||
| return convertStreamWithThinking(jsonlContent) | ||
| .filter { event -> event.isObject() } | ||
| .map { event -> event.getObject()!! } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Convert streaming text with thinking blocks into StreamingEvent objects. | ||
| * Supports both object lines and thinking blocks. | ||
| * Uses resilient error handling - logs warnings for individual line failures but continues processing. | ||
| */ | ||
| fun convertStreamWithThinking(text: String): Flux<StreamingEvent<T>> { | ||
| return Flux.fromIterable(text.lines()) | ||
| .filter { it.isNotBlank() } | ||
| .handle { line, sink -> | ||
| try { | ||
| when { | ||
| StreamingUtils.isThinkingLine(line) -> { | ||
| val thinkingContent = StreamingUtils.extractThinkingContent(line) | ||
igordayen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| sink.next(StreamingEvent.Thinking(thinkingContent)) | ||
| } | ||
| else -> { | ||
| val result = super.convert(line) | ||
| if (result != null) { | ||
| sink.next(StreamingEvent.Object(result)) | ||
| // Continue processing other lines | ||
| } | ||
| } | ||
| } | ||
| } catch (e: Exception) { | ||
| sink.error(e) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Override format to request JSONL instead of single JSON. | ||
| * Inherits schema injection from parent but modifies instructions for streaming. | ||
| */ | ||
| override fun getFormat(): String = | ||
| """| | ||
| |Your response should be in JSONL (JSON Lines) format. | ||
| |Each line should contain exactly one JSON object matching the schema. | ||
| |Do not include explanations, only RFC7464 compliant JSON Lines, one per line. | ||
| |Do not include markdown code blocks or wrap in arrays. | ||
| | | ||
| |You may include thinking content ANYWHERE in your response using: | ||
| |<think>your reasoning here</think> | ||
| | | ||
| |Thinking blocks can appear before, between, or after JSON objects as needed for your reasoning process. | ||
| | | ||
| |JSON Schema for each object line: | ||
| |```${jsonSchema}``` | ||
| | | ||
| |Example output showing flexible thinking placement: | ||
| |<think>Let me analyze this step by step</think> | ||
| |{"field1": "value1", "field2": "value2"} | ||
| |<think>The next item requires different consideration</think> | ||
| |{"field1": "value3", "field2": "value4"} | ||
| |<think>Final thoughts on the results</think> | ||
| |{"field1": "value5", "field2": "value6"} | ||
| |""".trimMargin() | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have my doubts about the usability of this method, see my comments on
StreamingEventbelow.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replied above. thanks