kiota icon indicating copy to clipboard operation
kiota copied to clipboard

Unexpected UntypedNode from type union generated using Fern

Open jsundh opened this issue 2 years ago • 3 comments

What are you generating using Kiota, clients or plugins?

API Client/SDK

In what context or format are you using Kiota?

Nuget tool

Client library/SDK language

Csharp

Describe the bug

I am trying to use Kiota version 1.17.0 to generate a C# client for Langfuse, using the OpenAPI specification for their API and have run into an issue with the response type for one of the endpoints there, /api/public/v2/prompts/{promptName}.

Instead of using the specified Prompt model, Kiota generates a RequestBuilder that returns UntypedNode. At the same time, the generate command runs without any errors or warnings.

The model is missing a discriminator field, but adding that using the following diff applied to the attached OpenAPI specification does not change anything as far as I can see:

--- openapi-langfuse.yml	2024-08-13 16:13:19
+++ openapi-langfuse-2.yml	2024-08-13 16:13:19
@@ -56,6 +56,8 @@
             - $ref: '#/components/schemas/TextPrompt'
           required:
             - type
+      discriminator:
+        propertyName: type
       ### --------------------------------------------------
     BasePrompt:
       title: BasePrompt

Expected behavior

Either that the Prompt model is used in the generated code or some kind of warning emitted during generation.

How to reproduce

Run the following generate command using the attached, minimized OpenAPI specification:

kiota generate -l csharp -d ./openapi-langfuse.yml -c LangfuseClient -n Langfuse.ApiClient -o ./Langfuse/ApiClient --clean-output

Seen behavior:

  • Output from command:
    Generation completed successfully
    Client base url set to https://cloud.langfuse.com
    
    [...]
    
  • No models in the generated output since the only endpoint is not using any.
  • Langfuse/ApiClient/Api/Public/V2/Prompts/Item/WithPromptNameItemRequestBuilder.cs return Task<UntypedNode?> from the GetAsync method.

Open API description file

Edited 2024-08-14 16:20 UTC: The type property was not supposed to be in the ChatPrompt and TextPrompt models.

openapi: 3.0.1
info:
  title: langfuse
  version: ''
  description: >-
    [...]
paths:
  /api/public/v2/prompts/{promptName}:
    get:
      description: Get a prompt
      operationId: prompts_get
      tags:
        - Prompts
      parameters:
        - name: promptName
          in: path
          description: The name of the prompt
          required: true
          schema:
            type: string
        # Other parameters omitted for brevity
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Prompt'
        # Other response codes omitted for brevity
components:
  schemas:
    ### --------------------------------------------------
    # Expected but unused Prompt model
    Prompt:
      title: Prompt
      oneOf:
        - type: object
          allOf:
            - type: object
              properties:
                type:
                  type: string
                  enum:
                    - chat
            - $ref: '#/components/schemas/ChatPrompt'
          required:
            - type
        - type: object
          allOf:
            - type: object
              properties:
                type:
                  type: string
                  enum:
                    - text
            - $ref: '#/components/schemas/TextPrompt'
          required:
            - type
      ### --------------------------------------------------
    BasePrompt:
      title: BasePrompt
      type: object
      properties:
        name:
          type: string
        version:
          type: integer
        config: { }
        labels:
          type: array
          items:
            type: string
          description: List of deployment labels of this prompt version.
        tags:
          type: array
          items:
            type: string
          description: >-
            List of tags. Used to filter via UI and API. The same across
            versions of a prompt.
      required:
        - name
        - version
        - config
        - labels
        - tags
    ChatMessage:
      title: ChatMessage
      type: object
      properties:
        role:
          type: string
        content:
          type: string
      required:
        - role
        - content
    TextPrompt:
      title: TextPrompt
      type: object
      properties:
        prompt:
          type: string
      required:
        - prompt
      allOf:
        - $ref: '#/components/schemas/BasePrompt'
    ChatPrompt:
      title: ChatPrompt
      type: object
      properties:
        prompt:
          type: array
          items:
            $ref: '#/components/schemas/ChatMessage'
      required:
        - prompt
      allOf:
        - $ref: '#/components/schemas/BasePrompt'
  securitySchemes:
    BasicAuth:
      type: http
      scheme: basic
servers:
  - url: https://cloud.langfuse.com

Kiota Version

1.17.0+1eb16cd65853c17179e2dde3ae6098135deacf55

Latest Kiota version known to work for scenario above?(Not required)

No response

Known Workarounds

No response

Configuration

No response

Debug output

No response

Other information

Langfuse seems to use Fern to generate the API specification: https://github.com/langfuse/langfuse/blob/ed4d7ceb40ecd0f302edc07e86170f70f8d2e386/fern/apis/server/definition/prompts.yml#L106

This might very well not be a bug in Kiota, but rather some quirk in how Fern generates its typed union definitions, but it would still be good to know why it does not seem to work with Kiota.

jsundh avatar Aug 13 '24 15:08 jsundh

Hi @jsundh Thank you for using kiota and for reaching out. Based on this description, I'm not sure how a client is supposed to "guess" a fixed property constant should be used as a discriminator?

It might be worth trying to make edits to the description and see if you get a better experience:

components:
  schemas:
    ### --------------------------------------------------
    # Expected but unused Prompt model
    Prompt:
      title: Prompt
      oneOf:
        - $ref: '#/components/schemas/ChatPrompt'
        - $ref: '#/components/schemas/TextPrompt'
      discriminator:
        propertyName: type
        mapping:
          text:  '#/components/schemas/TextPrompt'
          chat: '#/components/schemas/ChatPrompt'

baywet avatar Aug 13 '24 17:08 baywet

Hi! Thank you for the quick response!

Yes, making the above edits as well as moving the type property to the TextPrompt and ChatPrompt models makes it work and could serve as a workaround, definitely! Note: The type property is not part of the TextPrompt and ChatPrompt models in the original description from https://cloud.langfuse.com/generated/api/openapi.yml - I accidentally included them from some testing in the description posted above.

However, since this is a third-party API specification, I would like to avoid any changes to the description if possible and I initially thought that Kiota could help bridge that gap by supporting more description variations.

That said, having looked at this some more throughout the day, I don't think this is on Kiota to handle; Having to support a separated discriminator property that is only used during parsing and then omitted from the final model seems quite complicated. Especially when simply having it in the referenced model works just fine.

For completeness, here are some smaller examples that I worked up to describe the wanted behavior:

Description 1 - with the discriminator in the referenced model (currently works as expected):

openapi: 3.0.1
info:
  title: Discriminator in referenced model (1)
  version: ""
paths:
  /example:
    get:
      responses:
        "200":
          description: ""
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ExampleResult"
components:
  schemas:
    ExampleResult:
      oneOf:
        - $ref: "#/components/schemas/ExampleA"
        - $ref: "#/components/schemas/ExampleB"
      discriminator:
        propertyName: type
        mapping:
          a: "#/components/schemas/ExampleA"
          b: "#/components/schemas/ExampleB"
    ExampleA:
      type: object
      properties:
        type:
          type: string
          enum:
            - a
        values:
          type: array
          items:
            type: integer
    ExampleB:
      type: object
      properties:
        type:
          type: string
          enum:
            - b
        data:
          type: string

Description 2 - with the discriminator in an inline model, separately from the referenced model (currently results in UntypedNode):

openapi: 3.0.1
info:
  title: Discriminator in inline model (2)
  version: ""
paths:
  /example:
    get:
      responses:
        "200":
          description: ""
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ExampleResult"
components:
  schemas:
    ExampleResult:
      oneOf:
        - type: object
          allOf:
            - type: object
              properties:
                type:
                  type: string
                  enum:
                    - a
            - $ref: "#/components/schemas/ExampleA"
        - type: object
          allOf:
            - type: object
              properties:
                type:
                  type: string
                  enum:
                    - b
            - $ref: "#/components/schemas/ExampleB"
      discriminator:
        propertyName: type
        mapping:
          a: "#/components/schemas/ExampleA"
          b: "#/components/schemas/ExampleB"
    ExampleA:
      type: object
      properties:
        values:
          type: array
          items:
            type: integer
    ExampleB:
      type: object
      properties:
        data:
          type: string

The wanted result from kiota generate using description 2 would then be:

  • The same ExampleResult model as from description 1 with just the ExampleA and ExampleB properties.
  • ExampleA and ExampleB does not contain the discriminator property type.

This wouldn't completely bridge the gap though, because the Fern-generated OpenAPI description does not specify that the type property is the discriminator (it doesn't have a discriminator block). To then also assume that the only inline property should be used as the discriminator definitely seems like a step too far in a general client code generator (at least to me).

Thank you for your time. I consider this matter closed unless you want to pursue it as a feature request.

jsundh avatar Aug 14 '24 16:08 jsundh

Thank you for the additional information. Closing. Let us know if you have any additional comments or questions.

baywet avatar Aug 14 '24 16:08 baywet